473,657 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interesting and impossible NullPointerExce ption

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.
Jul 17 '05 #1
4 4067
Liz

"gabryh" <fa****@netvisi on.net.il> wrote in message
news:99******** *************** ***@posting.goo gle.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.


because value = null;
Jul 17 '05 #2
> public static boolean isEmpty(String value) {
return ((value == null) || (value.trim().e quals("")));
}


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

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

--
Luca Paganelli
ICQ# 52629494
Jul 17 '05 #3
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 *
Jul 17 '05 #4
Ian Shef <in*****@avoidi ng.spam> wrote in message news:<Xn******* *************** ******@138.126. 254.210>...
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("") ?

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
8570
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 code below: RequestDispatcher dispatcher = myConfig.getServletContext().getRequestDispatcher(view); dispatcher.forward(request, response); The funny thing is that it will do most of the database work it's supposed to, but then gives me the...
0
4409
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 show up via tomcat. I'm running on Fedora Core 2, tomcat-4.1.27-13. I haven't noticed any difference if I start apache httpd-2.0.50-2.1 or not. Here are the errors: MsgAjp - -BAD packet signature 18245
2
3076
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 sun.awt.RepaintArea.paint(RepaintArea.java:177) at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260) at java.awt.Component.dispatchEventImpl(Component.java:3678) at java.awt.Container.dispatchEventImpl(Container.java:1627)
13
1961
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try{
1
2443
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 TextField t1, t2, t3;
2
2273
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events -> Action -> actionPerformed. The auto-generated code appears for a brief moment before it...
1
3187
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events -> Action -> actionPerformed. The auto-generated code appears for a brief moment before it...
1
17542
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 before, lending proof to Einstein's statement: “Only two things are infinite, the universe and human stupidity ...”. Main Cause Dereferencing null. This is by far the more common cause of getting the exception. Reference types in both...
3
3328
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 { ArrayList<String> dictionary; ArrayList<String> encryptedText;
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8621
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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 we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.