• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using an object in a 3rd class that references an object from another object

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am having an impossible time understanding how I need to reference an object which is referenced in another object in Main.

Here's my code:




Customer references Customer ID:


And I need my main to call this:



I think what I need to do is give getCustomer(Customer Id, Address mailingAddress, Connection con), but that isn't good enough, I tried CustomerId id and it was sort of satisfied with that but then the code blows up at setInt(1,id) and I also can't figure out c.setMailingAddress to get the string of rs into the object of mailingAddress.

Thanks! I have never posted an question before, so I apologize if my format is off. I love feedback so let me know what I am doing wrong.
-Meg
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Meg,

You need to slow down and TellTheDetails, you are a bit 'all over the place' which makes it hard to answer your question. First, you are having some sort of problem with the code you provided. What is the problem? Do you get an error message? Does the application crash? Does it do the wrong thing? Tell us, in detail (using copy and paste for the error message when appropriate) so we can figure out what the issue is and reply constructively.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch Meg. Have you written anything at all in your main() method? If yes, can you show that to us and point out where it is exactly that you are stuck?
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, sorry. It is NOT my main method. Here is the entire class:

I think my problem starts on line 52 with Customer c = new Customer();

getCustomer is trying to see if a customer exists in a database. An id is given (as an int), and it checks to see if the id matches any current customers. If there is no match, a new customer should create starting on line 52. My problem is that I don't know how to set that data when my customer class references other objects in other classes (id from CustomerId or mailingAddress from Address). I understand how customer.setFirstName works, the class customer knows that firstName is a string because it is defined as a string in the customer class. id and mailingAddress are objects in the customer class, and I don't know how to reference them so that the id (an int) I am dealing with can be cast into an object.

Eclipse has line 54 underlined in red and is stating the following:

+change method 'setId(CustomerId)' to 'setId(int)'
+change type of 'id' to 'CustomerId'
+create method 'setId(int) in type 'Customer'

Line 58 has the following suggestions:
+change method 'setMailingAddress(Address)' to 'setMailingAddress(String)'
+create method 'setMailingAddress(String) in 'Customer'



 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wanted to point this out:

Meg Berg wrote:Eclipse has line 54 underlined in red and is stating the following:

+change method 'setId(CustomerId)' to 'setId(int)'
+change type of 'id' to 'CustomerId'
+create method 'setId(int) in type 'Customer'

Line 58 has the following suggestions:
+change method 'setMailingAddress(Address)' to 'setMailingAddress(String)'
+create method 'setMailingAddress(String) in 'Customer'


Those aren't the error messages, those are suggestions on how to fix the errors. What are the actual error messages? If you can't figure out how to get Eclipse to show you the actual error messages in a format that you can copy and paste, then ditch the IDE and go to the command prompt and compile the code manually.


As to your problem:

Meg Berg wrote:... An id is given (as an int), ...My problem is that I don't know how to set that data when my customer class references other objects in other classes (id from CustomerId or mailingAddress from Address). ... id and mailingAddress are objects in the customer class, and I don't know how to reference them so that the id (an int) I am dealing with can be cast into an object



Well, the id (as an int) can't be cast to a CustomerId because an int is not a sub-type of CustomerId. Do you know how to make a CustomerId?
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve,

I think this is the error you are talking about:

Line 54 says: The method setId(CustomerId) in the type Customer is not applicable for the arguments (int)

Line 58 says: The method setMailingAddress(Address) in the type Customer is not applicable for the arguments (String)

I thought I understood how to create a CustomerId but I guess not.

Thanks.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how do you create an instance of a Class in general? For example, how do you create a Customer instance?
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As in CustomerId cusId = new CustomerId(); ?

Should it look like this?


I hope that is right because that makes sense to me.

For address, however, I don't seem to be able to apply the same logic.

I tried:



But get the error:
- The constructor Address(String) is undefined

Which is true, address is:



I realize there is a little bit of a mismatch here because I have 'address' as one field from the result set and yet 'Address' actually has several variable, but let's say that I was only looking to pull in the field of 'state' from my database and so my rs was actually only one variable. How can I call that?

I tried:
String state = rs.getString(3);
Address addy = new Address(state);
c.setMailingAddress(addy);
but it fails with an error on line 'Address addy...' with a reason of
- The constructor Address(String) is undefined
I also tried:
Address addy = new Address (rs.getString(3));

but I get the same error
- The constructor Address(String) is undefined

I think I need to specifically set Address(street) to rs.getString(3) but I can't see how to do that.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct with the CustomerId.

For the Address, the class does not have a constructor which takes the address as a String. It only has an empty constructor. That means you will have to create an empty Address (one without any of the address parts) and set the parts you know about after creating the Address itself. How would you do that?
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,

I think I've got it:



If that looks okay, I can say that you taught me more this afternoon than 3 semester of Java. Thanks!!
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha, that is correct. Congrats! It is all about finding out what you have and how to get from that to what you want - like a big puzzle sometimes
 
Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic