Results 1 to 15 of 15
  1. #1
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default Null pointer exception

    Hello, I'm trying to create a class called "Bottle" and it's a class were give values to each Bottle instance and then you can do things like multiplying, dividing, adding, subtracting them. I'm having trouble with my add/subtract/divide/multiply methods, since every time I try to run them I get a null pointer exception. I had also created a method called checkNumber, to make sure the number value for Bottle classes is not null. Please help me resolve this issue. The program is compiled like this:

    package bottleclass;

    import java.util.Scanner;

    /**
    *
    * @author
    */
    public class Bottle {

    public int number;
    public int bottle;
    public Bottle other;
    public Bottle answer;

    private int read() {

    int check;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of bottles: ");
    check = input.nextInt();
    if(check > 0) {
    number = check;
    }
    else {
    System.out.println("Number you had entered is invalid");
    }

    return number;

    }

    private void checkNumber() {

    System.out.println(number);
    }

    private void setInt(int number) {
    this.number = number;
    }

    private void setBottle(Bottle other) {
    this.other = other;

    }

    private Bottle add(Bottle other) {
    this.other = other;

    answer.number = this.number + other.number;
    return answer;
    }

    private Bottle subtract(Bottle other) {

    this.other = other;


    answer.number = this.number - other.number;
    return other;

    }



    public static void main(String[] args) {
    Bottle b1 = new Bottle();
    b1.read();
    b1.checkNumber();
    Bottle b2 = new Bottle();
    b2.read();
    b2.checkNumber();
    b2 = b2.add(b1);
    Bottle BottleAve = new Bottle();

    //BottleAve = BottleAve.subtract(b2)


    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    every time I try to run them I get a null pointer exception.
    Please copy full text of error message and paste it here.

    Also please wrap your code in code tags to preserver formatting. See: http://www.java-forums.org/misc.php?do=bbcode#code

  3. #3
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Ok, let me try to post it:

    The error is:
    PHP Code:
    Exception in thread "main" java.lang.NullPointerException
            at bottleclass.Bottle.add(Bottle.java:55)
            at bottleclass.Bottle.main(Bottle.java:78)
    Java Result: 1
    PHP Code:
    package bottleclass;
    
    import java.util.Scanner;
    
    /**
     *
     * @author    
     */
    public class Bottle {
        
        public int number;
        public int bottle;
        public Bottle other;
        public Bottle answer;
        
        private int read() {
            
            int check;
            Scanner input = new Scanner(System.in);
              System.out.println("Enter the number of bottles: ");
              check = input.nextInt(); 
              if(check > 0) {
                  number = check;
              }
              else {
                  System.out.println("Number you had entered is invalid");
              }
              
              return number;  
            
        }
        
        private void checkNumber() {
           
            System.out.println(number);
        }
        
        private void setInt(int number) {
            this.number = number;
        }
        
        private void setBottle(Bottle other) {
            this.other = other;
            
        }
        
        private Bottle add(Bottle other) {
            this.other = other;
            
            answer.number = this.number + other.number;
            return answer;
        }
        
        private Bottle subtract(Bottle other) {
            
            this.other = other;
            
            
            answer.number = this.number - other.number;
            return other;
            
        }
        
       
        
        public static void main(String[] args) {
            Bottle b1 = new Bottle();
            b1.read();
            b1.checkNumber();
            Bottle b2 = new Bottle();
            b2.read();
            b2.checkNumber();
            b2 = b2.add(b1);
            Bottle BottleAve = new Bottle();
            
            //BottleAve = BottleAve.subtract(b2)
            
           
        }
    
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    java.lang.NullPointerException
    at bottleclass.Bottle.add(Bottle.java:55)
    What variable is null at line 55? Find that variable. Has it ever been given a value? Back track in your code to see why is doesn't have a value.

  5. #5
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What variable is null at line 55? Find that variable. Has it ever been given a value? Back track in your code to see why is doesn't have a value.
    At line 55 I believe the variable is called answer.number

    It's a class reference variable that has int number variable attached to it, and I though by using operations of two variable and assigning it to that variable would give it the value...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by DBaskov View Post
    At line 55 I believe the variable is called answer.number

    It's a class reference variable that has int number variable attached to it, and I though by using operations of two variable and assigning it to that variable would give it the value...
    Look back and see where or if you have you initialized "answer"?

  7. #7
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Look back and see where or if you have you initialized "answer"?
    I made it into an instance variable, doesn't that initializes it or do I need to assign a value regardless?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    doesn't that initializes it
    The compiler will initialize primitives like int and boolean.
    Otherwise you are responsible.
    What data type is answer?

  9. #9
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The compiler will initialize primitives like int and boolean.
    Otherwise you are responsible.
    What data type is answer?
    The answer is a class reference variable: Bottle answer;
    If I should assign value to it, what should it be or how? I never really dealt with class reference variable that much to be honest...

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    If I should assign value to it, what should it be or how?
    If you have no use for the variable, take it out of your code.
    If you do have a use for it, then what is that usage?

    The "how" is by using an assignment statement with new:
    AClass yourVar = new AClass(); // create an instance of the AClass class

  11. #11
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    If you have no use for the variable, take it out of your code.
    If you do have a use for it, then what is that usage?

    The "how" is by using an assignment statement with new:
    AClass yourVar = new AClass(); // create an instance of the AClass class
    Well what I'm trying to do is create a method called "add" for say that returns a "Bottle(class reference variable)" and I want to take one instance of a class Bottle and add it to another by using one of the instances as a parameters, I thought by adding those two instances and assigning them to variable called
    "answer" and returning it would give me the value of those two object instances being added...

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I thought by adding those two instances and assigning them to variable called
    "answer" and returning it would give me the value of those two object instances being added
    The code you posted does not do that.
    The assignment was not to the variable answer. To assign a value to answer you would code:
    answer = <value to put in answer>
    Your were adding the contents of variables with two classes and storing the value inside of another instance of the class. However, you have never created that instance (it had a null value) so you got the NullPointerException.
    You need to create an instance of the Bottle class and put its reference in the answer variable.

  13. #13
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The code you posted does not do that.
    The assignment was not to the variable answer. To assign a value to answer you would code:
    answer = <value to put in answer>
    Your were adding the contents of variables with two classes and storing the value inside of another instance of the class. However, you have never created that instance (it had a null value) so you got the NullPointerException.
    You need to create an instance of the Bottle class and put its reference in the answer variable.
    Well I couldn't assign the solution of adding those two variables because they have int(number) variable in them, and java does not allow assigning int variables to object referenced variable unless I use something like this:

    answer.number = this.number + other.number;
    return answer;

    It won't let me return answer.number , nor would it let me do: answer = this.number + other.number because number is an int. I declared variable Bottle answer; as an instance variable btw.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What does the add() method return? Where will you get the thing to be returned?
    Answer: YOU WILL HAVE TO CREATE ONE.
    See post #8 for some syntax for creating an instance of a class.

  15. #15
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Hmm, It seems to be working now. I didn't it think it was necessary for me to instantiate the instance of a class in the method because I already had it as an instance variable. Basically what I did is put: Bottle answer = new Bottle() in that method and it seems to be working fine. Thanks for the help. Hopefully I won't need any more for a while lol.

Similar Threads

  1. Null pointer exception
    By jessie in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 02:58 PM
  2. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  3. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 PM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  5. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM

Tags for this Thread

Posting Permissions

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