• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I fix these NullPointerExceptions?  RSS feed

 
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all! I hope that some of you can help me! I'm working on a Java project involving the Stack API, and I've run into some problems. I try to run my code, and I get a NullPointerException, 4 times. I know that an NPE is the result of not initializing an object, and that the object references nothing so I can't call methods on it. However, I'm stuck and I don't know how to initialize these objects how I'd like to. The NPE is present on lines 124, 230, 264, and 280.

Here's my code:

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ty Roner wrote:The NPE is present on lines 124, 230, 264, and 280.


But those aren't the line numbers which are in your post, are they? Because a couple of those lines couldn't possibly throw an NPE.

And anyway the way to avoid the NPE is going to depend on your design. It's likely to be different in different places. So how about if you pick one of the problem lines and tell us what the line number of that line is in the post?
 
Ty Roner
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might have messed up which lines the NPE are on.

The first one actually is on line 124, the operand Object isn't initialized. This is what I'm getting in Eclipse.
 
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line that throws an exception when I run it is



After adding some print statements (below), it become obvious that there is a null in your stack, and when you pop it out of your stack, you assign the null to the variable operand. You then attempt to call the equals() method on your null pointer.

 
Ty Roner
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do I just need to get rid of the null in my stack? What should I attempt to do now? Sorry if this is a dumb question.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider your design. Does it require or permit nulls to be put into that stack? If the answer is no, then you should look for code which is putting the nulls into the stack and fix it to not do that.
 
Ty Roner
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I don't think I need the nulls put into the stack. I believe that the code that's causing this is setting operator Object and result Object to null. What should I do to these instead of setting them to null?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ty Roner wrote:Alright, I don't think I need the nulls put into the stack. I believe that the code that's causing this is setting operator Object and result Object to null. What should I do to these instead of setting them to null?

As an initial value? When it comes to objects, you may not have a choice (although you could possibly use '0.0' for a Double). The problem is not usually with the null itself, but with the fact that you're trying to use an object that is null - ie, before you've given it a value.

I'm also not sure about making your expression stack a Stack<Object>, because that would suggest that you're having to use dispatch code when you pop something off. Do you have any expression parts that aren't Strings?

Winston
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must have put a null onto the stack. If you try to get anything off an empty stack, you get a different Exception.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ty Roner wrote:Alright, I don't think I need the nulls put into the stack. I believe that the code that's causing this is setting operator Object and result Object to null. What should I do to these instead of setting them to null?


I don't know. Why did you decide to set them to null in the first place?

(Bear in mind that I'm not looking at your code because there is nearly 300 lines of it. If you want to point to particular parts of the code, then go ahead and do so.)
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check NPE before using particular object reference.
for example:
when you are going to use "operand" at line 124


I would suggest you to check this condition even you are sure there is no null object in your stack. Its just for safe hand.

Paul Clapham wrote:
Ty Roner wrote:The NPE is present on lines 124, 230, 264, and 280.


But those aren't the line numbers which are in your post, are they? Because a couple of those lines couldn't possibly throw an NPE.

And anyway the way to avoid the NPE is going to depend on your design. It's likely to be different in different places. So how about if you pick one of the problem lines and tell us what the line number of that line is in the post?
 
Ty Roner
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies everyone. I've gone ahead and changed the design of my product after talking with my professor, but now I've got a new exception, a ClassCastException. I don't think I should make a new thread so I'm just adding on to this thread. It's telling me that I can't cast a string to a character. I'm not entirely sure what to do now, should I change everything to one type? Here's the code snippet:




 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ty Roner wrote:
It's telling me that I can't cast a string to a character. I'm not entirely sure what to do now, should I change everything to one type? Here's the code snippet:


I'm assuming that you have a one character String on your stack instead of a character. So, instead of,



you can try:



It would be better, however, if you had put a Character on the stack in the first place.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singh Kuldeep wrote:You can check NPE before using particular object reference.
for example:
when you are going to use "operand" at line 124


I would suggest you to check this condition . . .
I am not sure I agree there. There are some instances where a reference can legitimately point to null, but I don't think this is one of thse circumstances.

An example is a tree; every time you add a node to a binary tree, you add another null, so you will need null tests somewhere. But in this instance, the operator ought not to be null, so there is a mistake somewhere in the program. And such mistakes can be bl**d* difficult to find.
 
Ty Roner
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:
Ty Roner wrote:
It's telling me that I can't cast a string to a character. I'm not entirely sure what to do now, should I change everything to one type? Here's the code snippet:


I'm assuming that you have a one character String on your stack instead of a character. So, instead of,



you can try:



It would be better, however, if you had put a Character on the stack in the first place.


I did this, and now I have an NPE again.

I emailed my Professor asking the same question, and he told me:

"you need to know what is the class type of the popped object, before you can cast it
better to point to Object ref"

What object is he referencing?

Edit: I set double to 0.0, and the NPE has vanished. However, now I'm getting an EmptyStackException. I guess somewhere I haven't utilized my stack properly? Now I'm going in circles haha.

Edit 2: Okay now I'm confused. I redid everything from the beginning in Eclipse, but I'm getting this error:



But, the weird thing is, when compile my code in Terminal, I get these errors:



What do I do?

Edit 3:

I fixed it all! Works like a charm now!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you are creating a parser. What you have is something like 123 + 456 × 789.

You go from left to right and you find, “123. Now that's a number,” so you push 123‑NUMBER onto the stack. Then you get +‑OPERATOR. Why not have a Token class with fields for value and type? You can create an enum for NUMBER/IDENTIFIER/OPERATOR. Now you only have Token objects and you can get their type easily. No need to cast objects because everything is the same type. Remember casts are iffy error‑prone things.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!