• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mysterious Null pointer exception  RSS feed

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I get Null pointer in my code and I am unable to debug it.
When I watch the different expressions thay seems ok.

Given:


I get NullPointerException when the code written like this:


But when the code written like this:


Everything is working fine...
Can someone please tell me what am I doing wrong?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the error message, including the stack trace, look like exactly?

The stack trace pinpoints exactly where the NullPointerException happens. From your code it's hard to tell, because it could be happening inside the getShape method, or in the indexOf method, or somewhere else. Can you post some more of your code? (Your first snippet isn't real, compilable code!).

Sometimes a NullPointerException can happen during unboxing; for example you have an Integer that is unboxed to an int; if the Integer is null, you'll get an NPE. Don't know if that's the case here, it's just an example of where you can sometimes get a puzzling NPE.

Did you try stepping through it line by line with a debugger? That's often a good way of discovering exactly what happens. IDEs like Eclipse and NetBeans have very good built-in debuggers with which you can step through your code statement by statement.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:
Did you try stepping through it line by line with a debugger? That's often a good way of discovering exactly what happens. IDEs like Eclipse and NetBeans have very good built-in debuggers with which you can step through your code statement by statement.

Yes I am debugging with eclipse, I set breakpoint to NullPointerException
Eclipse stopped at the "indexOf" line but when I evaluate each variable they seems ok, and evaluating all the expression [shapes.indexOf(getShape(shapeId))] is also working ok.

The code is just a mock code for better understanding, but its reflecting the real code, getShape don't contain error I checked the method few times...
Thank you for your help,
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your real code multithreaded ?
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ireneusz Kordal wrote:Is your real code multithreaded ?

This is webApp running on Tomcat,
There is no Synchronization on this block but this code is accessed via multiple users/pages.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!