Null pointer Exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uhdam
    New Member
    • Mar 2009
    • 50

    #1

    Null pointer Exception

    Hi..

    I get a null pointer exception error. Please give me a solution as how to access this. The code snippet is as follows.
    Code:
     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
    
    }
    Last edited by Nepomuk; Apr 28 '09, 04:26 PM. Reason: Please use [code] tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Are you sure that your code compiles?
    Also read the NullPointerExce ption article.

    Comment

    • uhdam
      New Member
      • Mar 2009
      • 50

      #3
      Yes sir.. it does...

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Classes a and z do not compile.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by uhdam
          Yes sir.. it does...
          Don't lie. The code you've shown us doesn't compile; no discussion about it.

          kind regards,

          Jos

          Comment

          • devgupta01
            New Member
            • Jan 2008
            • 20

            #6
            Please correct your code and write code according to java recomendations

            Originally posted by uhdam
            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.prin tln(za.ax.gety( )); //null Pointer exeption

            }
            Correct code for class a and claas z as follows -

            Code:
            public class Z {
            	public static void main(String a[]){
                    A az = new A();
            System.out.println(az.ax.getY()); //null Pointer exeption
              }
            }
            
            class A
            {
                X ax;
                A(){
            	ax = new X();
            	ax.setY("Hai");
                  }
            
            }
            
            
            class X
            {
            	private String y;
            
            	public String getY()
            	{
            		return y;
            	}
            
            	public void setY(String m)
            	{
            		this.y = m ;
            	}
            
            }
            Now I think urn code will compile and run...

            Comment

            Working...