Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do we spot what will cause a NullPointerException?

 
Dan Acuff
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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()}"/>

 
Joachim Rohde
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic