tags:

views:

156

answers:

6

Is there an IDE or IDE plugin that would alert the developer for a possible NPE? For example:

Object[] x = getMyObjects();

if (x.length > 0){

   // do my stuff

}

It would be nice if the IDE alerts that x is not checked for a possible null value. It would save many engineering hours..

+6  A: 

My copy of Eclipse raises a warning for this by default.

John Topley
hmm.. I use NetBeans. Wondering if there is a plugin for it. +1 for you, thx.
srini.venigalla
Unfortunatly this warning is broken in eclipse (3.5 and before). It gets issued also for core like `if(chars==null) (chars = new char[128])[127] = ch;` although chars it can never be null here.
x4u
Interesting, my Eclipse did not raise a warning. I even tried to change the settings so that it warned for potential null values and it still didn't catch this as a warning.
Dan
OK I'm using version 3.5 - I would go with Find Bugs.
Dan
@ Dan: The code I posted above gets detected as definitive null pointer access by eclipse (I'm also on 3.5), not as possible null pointer access. You need to activate the "null pointer access" check to see it.
x4u
+1  A: 

in addition to John's answer...


Eclipse 3.4 gave me an excellent message, which was something like :

The variable can only be null at this point.

I couldn't believe it at first, but it was true. It was two expressions like :

if ((var == null) && (var.getProperty().equals("")) {

if ((var != null) || (var..getProperty().equals("")) {
KLE
+5  A: 

This is something that FindBugs can detect among many other usefull things. There is also a Eclipse plugin available.

x4u
+3  A: 

NullPointerExceptions are programming errors by nature.

What could you do if the given array is null? A. skip the check. A better alternative would be to return an empty array:

public Object [] getMyObjects() {
     /// do a lot of stuff 

    if( nothingToReturn() ){
        return new Object[0];
     } else {
         return nonNullArray;
     }
}

So

if( x.length > 0 ) 

Never throws Npe in first place.

Although It would be nice, very soon you'll realize your code will be unreadable.

That's why "encapsulation" is such an important concept in OOP ( that way other object's won't nullify your data ) .

I think it's fair to check if external classes return null or not ( which by the way should be well documented ) but for internal methods, the real problem is, the own object doesn't know the state of its own instance variables.

NOTE This was too large for a comment, that's why I provided a CW answer instead.

OscarRyz
Tip: if you are going to return an empty array, create one final static one and return that.
rsp
@rsp: Indeed, following the NullObject pattern. You could add that to the answer.
OscarRyz
+3  A: 

You might want to start with adding the FindBugs plugin to check for this kind (and many more) bugs on every save. And if you haven't already, add the CheckStyle plugin too :-)

rsp
OP told in one of the comments he was using Netbeans.
BalusC
+1  A: 

Eclipse 3.5 (and AFAIR 3.4, too) has two compiler options for Nullpointers.

You can find them at Window -> Preferences -> Java -> Compiler -> Error/Warnings -> Potential programming problems.

My preferred settings are Error for Null pointer access (these are definitly raising Exceptions) and Warning for the Potential null pointer access.

Hardcoded