I am learning java from core java volume 1 book and very new to java.sorry for confusing title but I don't what is going on. I had written a program which will print the information about a person on a screen. So I had created an array of object of Man class:-
Here is my Man class:-
package com.smit.man;
public class Man
{
private int age;
private long id;
private String name;
private double weight;
public Man(int aage,String aname,double aweight,long aid)
{
age =aage;
name = aname;
weight = aweight;
id = aid;
}
private Add add = new Add((name + "'s Shellter;"),(name + "'s hous's front road;"),(name + " colony;"),id+422);
public int getAge()
{
return age;
}
public long getId()
{
return id;
}
public String getName()
{
return name;
}
/**
* @return the weight
*/
public double getWeight()
{
return weight;
}
public void pall()
{
System.out.println
(
"Name = " +this.getName() +
" Age = "+this.getAge() +
" ID = " +this.getId() +
" Weight = "+this.getWeight()
);
add.pall();
System.out.println("\n\n\n");
}
}
This Man class Uses the Add class which is this :-
package com.smit.man;
/**
* @author smit
*
*/
public class Add
{
private String shellterN,Rd,colony;
private long pin;
Add(String a,String b,String c,long d)
{
this.shellterN = a;
this.Rd = b;
this.colony =c;
this.pin = d;
}
public String getShellterN()
{
return shellterN;
}
public String getRd()
{
return Rd;
}
public String getColony()
{
return colony;
}
public long getPin()
{
return pin;
}
public void pall()
{
System.out.println("ShellterName = " + this.getShellterN() +
" Road = "+this.getRd()+
" Colony= "+ this.getColony()+
" PIN = " + this.getPin());
}
}
And here is main method class:-
package com.smit.main;
import com.smit.man.*;
public class Mtest
{
public static void main(String[] args)
{
Man man [] = new Man[3];
man[0]= new Man(17,"Smit",55,34);
man[1] = new Man(100,"Master",60,400);
man[2] = new Man(200,"Guru",10,39);
for(Man a:man)
a.pall();
}
}
Whenever I run this in eclipse I get following output
Name = Smit Age = 17 ID = 34 Weight = 55.0
ShellterName = null's Shellter; Road = null's hous's front road; Colony= null colony; PIN = 422
Name = Master Age = 100 ID = 400 Weight = 60.0
ShellterName = null's Shellter; Road = null's hous's front road; Colony= null colony; PIN = 422
Name = Guru Age = 200 ID = 39 Weight = 10.0
ShellterName = null's Shellter; Road = null's hous's front road; Colony= null colony; PIN = 422
I don't want this null?? Why I am getting this null ??
private Add add = new Add((name + "'s Shellter;"),(name + "'s hous's front road;"),(name + " colony;"),id+422);
this line is outside of the constructor, so it gets called right when the object is creates (even before the constructor). You have to make the add to a private variable like all the others and init it at the end of the constructor.
When this line is called, the man's name isn't initialized yet, so it is a null. That's what creates the problem.
Because you're creating the private add
class-level member in the Man
class before the constructor executes. As soon as the class exists at all, class-level instantiations are executed. Then the constructor runs. So the name
member has its default value at that time.
Instantiate the value in the constructor after the name
value exists:
private Add;
public Man(int aage,String aname,double aweight,long aid)
{
age =aage;
name = aname;
weight = aweight;
id = aid;
add = new Add((name + "'s Shellter;"),(name + "'s hous's front road;"),(name + " colony;"),id+422);
}
Observe you code carefully. Right after public man
you have:
private Add add = new Add((name + "'s Shellter;"),(name + "'s hous's front road;"),(name + " colony;"),id+422);
This is not contained in any method, and so will initialize the add
member for all objects instantiated from this class with the values name
, age
etc. have at the start - which are of course null
, as you did not instantiate anything. The next time you use add
is to call add.pall
, where add
still contains these nulls.
Maybe you thought this works like pointers - when you change name
the name
in add will change as well - it doesn't.
What you probably want to do is declare add
but instantiate it in the constructor, like you do for the rest of the variables. Also, consider using inheritance here, that seems more appropriate for your code. Also, I would rename Add
to Citizen
or something.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.