fa****@netvision.net.il (gabryh) wrote in news:991b45ec.0405110213.2fb37572
@posting.google.com:
Hi,
The following code throws me NullPointerException.
....
public static boolean isEmpty(String value) {
return ((value == null) || (value.trim().equals("")));
}
Can anyone anywhere can suggest me how or why this happens?
Thanks.
|| is a shortcut operator, so if (value == null) then the expression should
return true and not evaluate the second half of the expression (that is
presumably causing the NullPointerException.
Some possibilities:
[I don't intend these to be rude, I just don't want to overlook anything.]
1) What you posted is not the same as what is in your program.
or
2) You need to compile (your class file is out of date with your java
source file).
or
3) The NullPointerException is happening somewhere else in your program.
Suggestion:
Break this method up into multiple lines, and put println statements before
every line. This will verify that your compilation is up to date, and help
pinpoint the location of the problem.
public static boolean isEmpty(String value) {
System.out.println("isEmpty -- Starting");
boolean t1 = (value == null) ;
System.out.print("isEmpty -- t1 is "); System.out.println(t1);
if t1 {
System.out.println("isEmpty -- returning for t1");
return true ;
}
System.out.println("isEmpty -- about to trim");
String t2 = value.trim();
System.out.println("isEmpty -- t2 is " & t2);
boolean t3 = t2.equals("");
System.out.print("isEmpty -- t3 is "); System.out.println(t3);
return t3;
}
Incidentally, have you considered using (0 == value.length()) instead of
(value.trim().equals("") ?
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *