• 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

How do we spot what will cause a NullPointerException?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote most of this code with the help of a SR. Java Developer. QA is reporting an occasional NullPointerException, what in this code could be causing that? I am calling it from JSF using <h:outputText escape="false" value="#{coreMetrics.buildCoreMetricsString()}"/>

 
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan Acuff wrote:QA is reporting an occasional NullPointerException, what in this code could be causing that?


You need to ask for the stacktrace and more details to reproduce the problem.
From what you posted, NPEs could occur in the following lines:
78, 79, 81, 103, 110, 111, 121, 122 ,123, 131, 138, 139, 140, 141, 142, 143, 158, 161, 163, 164, 166, 167, 168, 169, 173, 177, 178, 179, 180, 181, 182 and 183.
But I guess that's not really helpful.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can't know that for sure.
In the stack trace you see exactly where the NullPointerException is thrown.

For example it could happen at line 70 after the line

if the method getCleanCategoryName returns null
but,who knows...

Anyway when you get that exception look at the stack trace printed. It will exactly tell you what happened.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nicola is quite right: the exception stack trace will tell you what line the error occurs on. Then on that line, look for a variable name that's either immediately to the left of a dot (".") or immediately to the left of a square bracket ("["). Something in one of those positions is null. For each such variable name, you have to work backwards and find where you think it should have been assigned a value, and then determine if (1) it never actually was assigned a value in the first place, (2) the assignment wouldn't have happened until after the NPE occured, or (3) it was assigned a null value because a function returned null.

Hope this helps!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yuck, by the way--you mean this needs to be updated every time you add a URL?!
 
Dan Acuff
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes if you mean every time we add a new section of content, that section needs to get added.

What is the alternative?
 
reply
    Bookmark Topic Watch Topic
  • New Topic