Results 1 to 5 of 5
  1. #1
    chukks is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default newbe in Java and NullPointerException, why?

    Hi ,

    i have made this code in order to learn but when i run it, it throws a NullPointerException :

    Java Code:
    public class ClaseA {
        
        public int x[];        
        public ClaseB c; 
        
        public ClaseA (){        
            x = new int[2];        
            for (int i=0; i<x.length; ++i){
                x[i]= i;
            }            
            c= new ClaseB(x[1]);   
            show();    
        }    
        
        public void show(){        
           c.calc();
           c.showinClaseB();              
           System.out.println(c.a );            
        }
        
        public static void main (String args[]){      
            ClaseA p = new ClaseA();         
        }
    }
    Java Code:
    public class ClaseB {
        public int a;
        public ClaseA p ;
        
        public ClaseB(int x){        
            this.a= x;          
        }
        
        public void calc(){
            a++;        
        }
        
         public void showinClaseB(){        
           System.out.println(p.x[1]);
        }
    }
    I have two classes, A and B.

    How can i get the ClaseA attributes from ClaseB?

    This wouldnt be valid in claseB: ClaseA p = new ClaseA(); because it trhows an StackOverFLow error.

    Thanks in advance.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: newbe in Java and NullPointerException, why?

    when i run it, it throws a NullPointerException
    So what is the variable or other expression that is null? The stack trace should tell you: if you don't understand what it says, post it.

    ---

    It isn't all that clear what you want the code to do. You seem to have something along the lines of this, calling that, constructing the other... It may well be that the lack of some clear intention is what leads to null pointer exceptions and/or stack overflow errors. Or it could just be that I'm missing something: in that case what is the code supposed to do?

  3. #3
    chukks is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: newbe in Java and NullPointerException, why?

    Quote Originally Posted by pbrockway2 View Post
    So what is the variable or other expression that is null? The stack trace should tell you: if you don't understand what it says, post it.

    ---

    It isn't all that clear what you want the code to do. You seem to have something along the lines of this, calling that, constructing the other... It may well be that the lack of some clear intention is what leads to null pointer exceptions and/or stack overflow errors. Or it could just be that I'm missing something: in that case what is the code supposed to do?
    Thanks for the response.

    According to this code:

    Java Code:
     public void show(){        
           c.calc();
           c.showinClaseB();              
           System.out.println(c.a );
    as you can see, c.showinClaseB(); calls the method showinClaseB() , but:

    Java Code:
     public void showinClaseB(){        
            System.out.println(p.x[1]);
    It doesnt print the value of x[1] , and trhows a NullPointerException.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: newbe in Java and NullPointerException, why?

    When I compile and run your code this is what I see:

    Java Code:
    C:\Users\Educator\Desktop>javac -cp . Clase*.java
    
    C:\Users\Educator\Desktop>java -cp . ClaseA
    Exception in thread "main" java.lang.NullPointerException
            at ClaseB.showinClaseB(ClaseB.java:14)
            at ClaseA.show(ClaseA.java:17)
            at ClaseA.<init>(ClaseA.java:12)
            at ClaseA.main(ClaseA.java:22)
    
    C:\Users\Educator\Desktop>
    The output when it is run is what I was calling the stack trace. It says what the exception was and where it happened.

    You read them from the top down, and this stack trace indicates that the error occured on line 14 of ClaseB.java which is this line:

    Java Code:
    System.out.println(p.x[1]);
    Now you will get a NullPointerException of you use a variable (or other expression) as if it had a non null value when it is actually null. By "use" I mean dereference it with the "dot" operator, or access it as an array with []. In this line there are two candidates for the thing which is null: p and x. You can tell which one is actually null by printing their values:

    Java Code:
    public void showinClaseB(){
        System.out.println("About to print p.x[1] with p=" + p + " and x=" + x);
        System.out.println(p.x[1]);
    }
    Once you have figured out which variable it is that is null go back through your code to where you thought you had given it a non null values and figure out why that didn't happen.

  5. #5
    chukks is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: newbe in Java and NullPointerException, why?

    Thanks for the response but i already know whats the error.

    I havent inicializate the variable ClaseB in ClaseA, so i in order to access to the atributtes of ClaseA form ClaseB the only way to do is this:

    Java Code:
    public class ClaseB {
        public int a;
        public ClaseA p ;
     
        public ClaseB(int x, ClaseA p){ //now i have complete access to the attributes of claseA from claseB 
            this.a= x;          
            this.p = p;
        }
     
        public void calcu(){
            a++;        
        }
     
        public void mostrarenClase(){        
            System.out.println(p.x[1]);
        }          
    }
    so in ClaseA only you have to pass the parameters:

    Java Code:
     c= new ClaseB(x[1], this);
    It was simple , but i didnt realice.
    Anayweay thanks for the response AMpbrockway2.

Similar Threads

  1. PLEASE HELP ME-i got java.lang.NullPointerException
    By nandhinianand in forum NetBeans
    Replies: 4
    Last Post: 08-05-2011, 07:35 AM
  2. again, newbe question..
    By glina126 in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 11:23 PM
  3. Replies: 15
    Last Post: 01-13-2011, 07:28 PM
  4. Java LoginServlet NullPointerException
    By rushhour in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-14-2010, 11:13 AM
  5. java.lang.NullPointerException
    By Pombi in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •