aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

Tool to identify NPE in the code

T Su
Greenhorn

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
Greenhorn

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

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

Joined: Sep 21, 2011
Posts: 2408
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
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

That's the one!
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

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 - http://findbugs.sourceforge.net/bugDescriptions.html#NP_ALWAYS_NULL


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tool to identify NPE in the code