473,507 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interesting and impossible NullPointerException

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
4 4057
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8561
by: K S Aldebaraan | last post by:
I'm trying to submit a form with an action of a servlet, and a view equal to the same jsp page. I'm not sure what I'm doing wrong, but keep getting a NullPointerException on the second line of...
0
4404
by: Old-timer | last post by:
Not sure where else to post this. I'm sure I'm doing something wrong, but I wouldn't think a simple app would give me so much trouble. I've got a small test java class that I'm trying to have...
2
3066
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at...
13
1947
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ...
1
2401
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
2262
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
3182
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
17521
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
3
3321
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.