NullPointerCatching

check this code. When my tomcat asks if it is null WHEN IT IS NULL, the whole thing falls apart, fine otherwise.......?

String [] sizes = request.getParameterValues("sizes");

System.out.println(sizes);

try

{

if (!sizes.equals(null))//THE NULL LINE THAT CRASHES

{

request.setAttribute("sizes", sizes);

}

}

catch (java.lang.NullPointerException e)

{

//e.printStackTrace();

String[] sizes1 = new String[1];

sizes1[0]="Please Enter At Least One Selection";

request.setAttribute("sizes", sizes1);

}

[598 byte] By [burntcandlea] at [2007-10-3 2:07:29]
# 1
>the whole thing falls apartException stack?Program Crash?tomcat Crash?
r035198xa at 2007-7-14 19:06:21 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

> if (!sizes.equals(null))//THE NULL LINE

This line will always evaluate to true, or throw a null pointer exception. What it's trying to do is invoke the equals method on the object pointed to by the sizes reference. If sizes is null, there is no equals method for a null reference, and you get the null pointer exception. What you want is:

if (sizes != null)

BillKriegera at 2007-7-14 19:06:21 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...