Search This Blog

Saturday, September 5, 2009

NULL in Java:On the Job Problem

On the job problem: When I was integrating the development page one of my colleague I have come across hidden error in jsp.

We have to display hyphen if any string data type value is null or empty string.

So we used to write code like if( data_str==null || data_str.equals(“”))

He wrote like then if(data_str.equals(null) || data_str.equals(“”))
So we havn’t received any error but the execution is stopped when the control reach this statement.
If we run this application using java then we will get run time exception null pointer exception when we pass data_str as null
public class EqualTestProgram {

public static void main(String arg[])
String test=null;
System.out.println("before if");
if(test.equals(null)|| test.equals("") )
System.out.println("with in if");
System.out.println("after if");

i/p test=””;
before if
with in if
after if

i/p test=null;
Before if
Null pointer Exception

No comments:

Post a Comment