By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Interesting and impossible NullPointerException

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Liz

"gabryh" <fa****@netvision.net.il> wrote in message
news:99**************************@posting.google.c om...
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.


because value = null;
Jul 17 '05 #2

P: n/a
> public static boolean isEmpty(String value) {
return ((value == null) || (value.trim().equals("")));
}


When value = null, value.trim().equals("") is called over a null instance.
You'd better write as following:

return (
(value == null) ? true : value.trim().equals("")
);

--
Luca Paganelli
ICQ# 52629494
Jul 17 '05 #3

P: n/a
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 *
Jul 17 '05 #4

P: n/a
Ian Shef <in*****@avoiding.spam> wrote in message news:<Xn****************************@138.126.254.2 10>...
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("") ?

Thanks for the answer.
1. The code is up to date. The class of this function was not changed for months.
2. The function is given exactly as it is in our code.
3. After restarting the process it stopped happening.
4. Anyway I have thought that breaking the function into pieces might help here.
5. And I also changed to value.length() == 0.

Thanks,
Gabriel.
T
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.