Connecting Tech Pros Worldwide Forums | Help | Site Map

Null pointer Exception

Member
 
Join Date: Mar 2009
Posts: 50
#1: Apr 28 '09
Hi..

I get a null pointer exception error. Please give me a solution as how to access this. The code snippet is as follows.
Expand|Select|Wrap|Line Numbers
  1.  public class x
  2. {
  3.     private String y;
  4.  
  5.     public String gety()
  6.     {
  7.         return y;
  8.     }
  9.  
  10.     public void sety(String m)
  11.     {
  12.         this.y = m ;
  13.     }
  14.  
  15. }
  16.  
  17.  
  18. public class a
  19. {
  20.  
  21.     x ax = new x();
  22.     ax.sety("Hai");
  23.  
  24. }
  25.  
  26.  
  27. public class z
  28. {
  29.     a az = new a();
  30.     System.out.println(za.ax.gety()); //null Pointer exeption
  31.  
  32. }

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Apr 28 '09

re: Null pointer Exception


Are you sure that your code compiles?
Also read the NullPointerException article.
Member
 
Join Date: Mar 2009
Posts: 50
#3: Apr 28 '09

re: Null pointer Exception


Yes sir.. it does...
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Apr 28 '09

re: Null pointer Exception


Classes a and z do not compile.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Apr 28 '09

re: Null pointer Exception


Quote:

Originally Posted by uhdam View Post

Yes sir.. it does...

Don't lie. The code you've shown us doesn't compile; no discussion about it.

kind regards,

Jos
Newbie
 
Join Date: Jan 2008
Posts: 21
#6: Apr 28 '09

re: Null pointer Exception


Quote:

Originally Posted by uhdam View Post

Hi..

I get a null pointer exception error. Please give me a solution as how to access this. The code snippet is as follows.

public class x
{
private String y;

public String gety()
{
return y;
}

public void sety(String m)
{
this.y = m ;
}

}


public class a
{

x ax = new x();
ax.sety("Hai");

}


public class z
{
a az = new a();
System.out.println(za.ax.gety()); //null Pointer exeption

}

Correct code for class a and claas z as follows -

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Z {
  3.     public static void main(String a[]){
  4.         A az = new A();
  5. System.out.println(az.ax.getY()); //null Pointer exeption
  6.   }
  7. }
  8.  
  9. class A
  10. {
  11.     X ax;
  12.     A(){
  13.     ax = new X();
  14.     ax.setY("Hai");
  15.       }
  16.  
  17. }
  18.  
  19.  
  20. class X
  21. {
  22.     private String y;
  23.  
  24.     public String getY()
  25.     {
  26.         return y;
  27.     }
  28.  
  29.     public void setY(String m)
  30.     {
  31.         this.y = m ;
  32.     }
  33.  
  34. }
  35.  
  36.  
Now I think urn code will compile and run...
Reply