Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 1st, 2013, 08:57 AM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Java Composition

I am new to java and trying to use composition for the first time. I built this class called Person, who takes as an attribute a string name, and an object pAddress. It has two constructors and one copy constructor, a method to set the PAddress attribute and a method String toString:

Code:
public class Person {
	private String name;
	private Address pAddress;
	
    ///*constructor that gets the city, street, number, zip from    Address class*/

	public Person (String name, String city, String street, int number, int zip)
	{
		this.name=name;
		pAddress =new Address(city, street, number, zip);
	}
	public Person (String name, Address a)
	{
		this.name=name;
		pAddress = new Address (a);
	}
    //copy constructor
	public Person (Person other){
		this.name=other.name;
		this.pAddress= new Address (pAddress);

//set method for pAddress
    public void setPAddress(String city, String street, int    number, int zip)
	{
		pAddress.setCity(city);
		pAddress.setNumber(number);
		pAddress.setStreet(street);
		pAddress.setZip(zip);
	}
	public String toString(){
		return this.name  + ", " + pAddress;
}


However, when I try to print the String toString method, I get a NULL value for the object part. to be more specific:

Code:
public static void main (String[]args){
		Address Home=new Address("New York","5th Avenue", 112, 111);
		Person a=new Person("Carl",Home);
		System.out.println(a);


The last println comes out:

Code:
Carl, Address: nullst., 0, null


Thanks very much for every help. sorry for the long code, I'm new to java and it's tough to be specific. I promise to learn next time

Reply With Quote
  #2  
Old January 1st, 2013, 09:16 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
What variable has the null value? Why doesn't it have a non-null value?


Can you post the code for the Address class? That would be where the problem is.

Reply With Quote
  #3  
Old January 1st, 2013, 09:24 AM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
What variable has the null value? Why doesn't it have a non-null value?


Can you post the code for the Address class? That would be where the problem is.
.

Thanks for answering. The null value is received for the a value - an Address object.
The Address class:
Code:
public class Address {
private String city, street;
private int number, zip;
public Address (String c, String s, int n, int z){
    city=c;
    street=s;
    number=n;
    zip=z;
}
public Address (Address a)
{
    this.city=city;
    this.street=street;
    this.number=number;
    this.zip=zip;
}
public Address (String c, String s, int n)
{
    zip=0;
    city=c;
    street=s;
    number=n;
}


public String toString(){ 
if (zip==0)
    return "Address: " + street + "st., " + number + ", " + city ;
return "Address: " + street + "st., " + number + ", " + city  + ", " + zip;

}

Reply With Quote
  #4  
Old January 1st, 2013, 09:30 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
Quote:
null value is received for the a value - an Address object.

If the Address variable was null, the print out would be:
Carl, null

but the print out was:
Carl, Address: nullst., 0, null

that shows that the Address object was NOT null because "Address" comes from the Address class's toString() method. So the problem must be inside the Address class.

Try debugging the code by adding some printlns to the Address class to print out the values of the variables as they are set before they are used in the toString() method.


NOTE: The posted code does not compile without errors.
Please correct all the compiler errors and repost the code.

Last edited by NormR : January 1st, 2013 at 09:37 AM.

Reply With Quote
  #5  
Old January 1st, 2013, 09:38 AM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
If the Address variable was null, the print out would be:
Carl, null

but the print out was:
Carl, Address: nullst., 0, null

that shows that the Address object was NOT null because "Address" comes from the Address class's toString() method. So the problem must be inside the Address class.

Try debugging the code by adding some printlns to the Address class to print out the values of the variables as they are set before they are used in the toString() method.


NOTE: The posted code does not compile without errors.
Please correct all the compiler errors and repost the code.


Thank you. I think I got the answer - the problem is in the Address class indeed - I had to add:
Code:
this.city=getcity();
    this.street=getstreet();
    this.number=getnumber();
    this.zip=getzip();


In the code.

Thank you

Reply With Quote
  #6  
Old January 1st, 2013, 09:46 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
Does the code work now?

Reply With Quote
  #7  
Old January 1st, 2013, 09:47 AM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Does the code work now?

Yes, the output is:
Carl, Address: 5th Avenuest., 112, New York, 111

Reply With Quote
  #8  
Old January 1st, 2013, 09:49 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
Interesting. I would not expect the code posted in post#5 to work as you expected.
Can you post the new code.

Reply With Quote
  #9  
Old January 1st, 2013, 10:11 AM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Interesting. I would not expect the code posted in post#5 to work as you expected.
Can you post the new code.


It's exactly the same code, except this change:
Code:
public Address (Address a)
		{
		    this.city=a.getCity();
		    this.street=a.getStreet();
		    this.number=a.getNumber();
		    this.zip=a.getZip();
	
		}
    


Before the change, the getCity method wasn't there, just the "city" variable, and so on with the rest three variables.

Reply With Quote
  #10  
Old January 1st, 2013, 10:13 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
Quote:
except this change:

That's an important change.
Comments on this post
nerazzurri10 agrees: Thanks for the help

Reply With Quote
  #11  
Old January 1st, 2013, 10:33 AM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
That's an important change.

Yes, well now I've learned.

Do I have to make this change every time I set a private attribute, or just when I'm giving the object as a parameter to a method in OTHER class?

Thanks.

Reply With Quote
  #12  
Old January 1st, 2013, 10:38 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
Code:
this.city=a.getCity();

You need to use a reference variable (above it is a) to access the contents of another object.

Reply With Quote
  #13  
Old January 1st, 2013, 10:46 AM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Code:
this.city=a.getCity();

You need to use a reference variable (above it is a) to access the contents of another object.

Ok, thank you very much for your help.

Reply With Quote
  #14  
Old January 1st, 2013, 03:50 PM
nerazzurri10 nerazzurri10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 nerazzurri10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Hi, it's me again.

I've added a method that compares values within the object:
Code:
	public boolean sameAddress(Person p){
		Person b=new Person (p.getName(), p.getPAddress());
		if (p.getName()==name)
			return true;
		return false;


It's supposed to check if the Person's address is the name for both objects.

the main is here:
Code:
public static void main (String[]args){
		Address Home=new Address("New York","5th Avenue", 112, 111);
		Person a=new Person("Carl",Home);
		Person b=new Person ("Carl",a.getPAddress());
		System.out.println(b.sameAddress(a));


Even though they have the same address I get a FALSE answer to my method.

Does anybody know what's wrong with my code?

Thanks.

Reply With Quote
  #15  
Old January 1st, 2013, 04:10 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
What class variable(s) hold the addresses that you want to compare?

You should use the equals() method to compare the contents of Strings, not the == operator.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Java Composition

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap