Objective Oriented Programming

My Journey through the wonderful world of programming

Null pointers in Java

Posted by Suresh S on April 10, 2010

Ask any developer who has dirtied his hands with java programming, even slightly, “What is the most common (and probably the most ugliest of all exceptions) exception that he/she has come across during development?” I swear it will the infamous Null Pointer Exception [api doc] in java.

The best way to avoid is to have suitable null checks [ever heard of defensive programming?] in you code so that you are sure your program does not spit null pointer exception.

A couple of days back when I was debugging a method in a program that used a custom utility method to check for “null” value of an object. It was a real eye opener for me for I never really thought that that utility method that check for a null value could fool me like that.

Here is what happened…

[MyTestVO.java]

public Class MyTestVO implements Serializable{

private String loginName;

private String password;

public MyTestVO(String loginName, String password){

this.loginName = loginName;

this.password = password;

}

@Override

public String toString(){

StringBuffer sb = new StringBuffer();

sb.append("Login Name: "this.loginName);

sb.append("password:"this.password);

return null;

}

}

[CustomUtils.java]

public Class CustomUtils{

public static boolean isEmpty(Object value){

if(value == null){

return true;

}

if( value instanceof String ){

if( value.length == 0 ){

return true;

}

}

if(value instanceof Collections){

if( value.isEmpty( ) ){

return true;

}

}

if( value instanceof Map ){

if( value.isEmpty( ) ){

return true;

}

}

else if (value.toString() == null){

return true;

}

}

[MyTestClass.java]

public Class MyTestClass{

public static void main(String args[]) {

MyTestVO myTestVO = new MyTestVO("Test_Login_Name","Test_Password");

if(CustomUtils.isEmpty(myTestVO)){

System.out.println("My TestVO is null");

}

else {

System.out.println("My TestVO is not null");

}

}

}

Output: My TestVO is null

The above is a close simulation of the problem [the actual one involved populating the VO from the values fetched from a DB and processed by the business layer method]. However the issue is not that. The isEmpty() method in the custom utility class returned true!!

In such cases you first shot would be check you DAO method where you fetch the data from a Data access layer and see if the method returned a correct value or is it returning null.

The next check would be at the service layer method which returns this value object. But in my case both of them see to be working perfectly fine. Then where was the problem?

It really took me sometime to figure out that the culprit is the overridden toString() method in my VO class which seemed to always return null. The custom utility class returned true if the toString() on the object returned null (for me it makes no sense to have this check since I am not interested to investigate if I can invoke a toString() on a null object or not, when the object reference itself is null. However a check on the length when value passed is a string makes sense to me).

I do agree that the toString() method should have been implemented more carefully but then where is the fun if your program runs in one shot (so much for my naivety) . But I felt the utility classes’s isEmpty() is no less a culprit:)

Phew! Gotta be really weary when performing null checks!

They are the first things to be tested even before you hit the compile button. Having said that, I myself have done it innumerable times only to have seen the infamous stack trace I recognize with ease:)

P.S. I came across this video where the author speaks of static null analysis and how static null analysis plug in with Eclipse IDE fails at some places. The author also cites the scenarios in which a null pointer exception can occur.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.