fa****@netvisio n.net.il (gabryh) wrote in news:991b45ec.0 405110213.2fb37 572
@posting.google .com:
Hi,
The following code throws me NullPointerExce ption.
....
public static boolean isEmpty(String value) {
return ((value == null) || (value.trim().e quals("")));
}
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 NullPointerExce ption.
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 NullPointerExce ption 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.prin tln("isEmpty -- Starting");
boolean t1 = (value == null) ;
System.out.prin t("isEmpty -- t1 is "); System.out.prin tln(t1);
if t1 {
System.out.prin tln("isEmpty -- returning for t1");
return true ;
}
System.out.prin tln("isEmpty -- about to trim");
String t2 = value.trim();
System.out.prin tln("isEmpty -- t2 is " & t2);
boolean t3 = t2.equals("");
System.out.prin t("isEmpty -- t3 is "); System.out.prin tln(t3);
return t3;
}
Incidentally, have you considered using (0 == value.length()) instead of
(value.trim().e quals("") ?
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *