File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Tool to identify NPE in the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Tool to identify NPE in the code" Watch "Tool to identify NPE in the code" New topic

Tool to identify NPE in the code

T Su

Joined: Oct 27, 2011
Posts: 1
I am looking for a tool to find potential NPEs like the one below. In line 2, work address should have been checked against Null before calling the getZipCode() method.
I was hoping tools such as PMD, checkStyle or findBug had something for this but have not found one.
Can someone help?
Thanks much!

Person p = ...;
if(p.getWorkAddress().getZipCode() == '...'){
Abhi Trivedi

Joined: Oct 25, 2011
Posts: 4
IntelliJ IDEA has a code analyzer that can do that.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
I think you can get warning triangles with Eclipse, too.
Tim Moores

Joined: Sep 21, 2011
Posts: 2415
NPEs occur at runtime, so it's not surprising that static code checkers such as the ones you mention (which do not run the code) can't detect them. While there are code spots where it can be proven that an object will be null, and thus subsequent method invocations on it will cause an NPE, that's quite rare; a good IDE might even find those spots for you, so there'd be no point in additional tools.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

Eclipse won't find possible NullPointerExceptions like that one. It has some (but not perfect) checking for variables that may be null, but method return types - no.

I do believe there is one IDE (Netbeans? IntelliJ?) where you could add a custom annotation to the method to indicate that it could or could not return null. However, you'll be limited to that IDE if you use it, as it's a custom annotation, one that's not available for anyone but users of the IDE.

How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

Maybe Rob is referring to this: IntelliJ IDEA Nullable How-To?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

That's the one!
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 607

Findbugs spots a list of possible null deference, as Tim mentioned it won't catch everything but it does a fair job at giving you a warning at many possible places. It integrates with eclipse pretty nicely too ...
(I've seen it catch a few of those in some of our existing code base & we got those fixed!)

NP: Null pointer dereference (NP_ALWAYS_NULL)

A null pointer is dereferenced here. This will lead to a NullPointerException when the code is executed

More -

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
I agree. Here's the link:
subject: Tool to identify NPE in the code
It's not a secret anymore!