aspose file tools*
The moose likes Beginning Java and the fly likes How do we spot what will cause a NullPointerException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do we spot what will cause a NullPointerException?" Watch "How do we spot what will cause a NullPointerException?" New topic
Author

How do we spot what will cause a NullPointerException?

Dan Acuff
Ranch Hand

Joined: Jul 13, 2009
Posts: 62
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

Joined: Nov 27, 2006
Posts: 423

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

Joined: Apr 10, 2010
Posts: 308
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.


Bye,
Nicola
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!


[Jess in Action][AskingGoodQuestions]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yuck, by the way--you mean this needs to be updated every time you add a URL?!
Dan Acuff
Ranch Hand

Joined: Jul 13, 2009
Posts: 62
Yes if you mean every time we add a new section of content, that section needs to get added.

What is the alternative?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do we spot what will cause a NullPointerException?