Results 1 to 15 of 15
Thread: Null pointer exception
- 07-10-2011, 08:44 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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)
}
}
- 07-10-2011, 09:29 PM #2every time I try to run them I get a null pointer exception.
Also please wrap your code in code tags to preserver formatting. See: http://www.java-forums.org/misc.php?do=bbcode#code
- 07-10-2011, 11:04 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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) } }
- 07-10-2011, 11:13 PM #4java.lang.NullPointerException
at bottleclass.Bottle.add(Bottle.java:55)
- 07-10-2011, 11:20 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
-
- 07-10-2011, 11:24 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
- 07-10-2011, 11:27 PM #8doesn't that initializes it
Otherwise you are responsible.
What data type is answer?
- 07-10-2011, 11:30 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
- 07-10-2011, 11:33 PM #10If I should assign value to it, what should it be or how?
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
- 07-10-2011, 11:39 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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...
- 07-10-2011, 11:44 PM #12I 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 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.
- 07-10-2011, 11:57 PM #13
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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.
- 07-11-2011, 12:00 AM #14
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.
- 07-11-2011, 12:16 AM #15
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
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
-
Null pointer exception
By jessie in forum New To JavaReplies: 5Last Post: 02-08-2011, 03:58 PM -
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 07:48 PM -
Null Pointer exception
By diegoyj in forum New To JavaReplies: 7Last Post: 01-29-2010, 05:17 PM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 07:33 PM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 08:22 PM
Bookmarks