• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mysterious Null pointer exception

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your real code multithreaded ?
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic