| Author |
aString;equals(bString) on null-object
|
Patrick Muls
Greenhorn
Joined: Aug 08, 2001
Posts: 25
|
|
|
The aString.equals(bString) method throws a NullPointerException when aString is null. While that is probably the best solution using other objects, I wonder why it is not intercepted for a String object as it is obvious that the intented result is false.
|
 |
Gabriel White
Ranch Hand
Joined: Mar 02, 2003
Posts: 233
|
|
Hey Pat, could you post your code: remember to use the code tags Thanks Steve
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
Regardless of what type of object a reference 'points' to, an attempt to dereference a null reference will lead to an NPE. Simply because the reference happens to be to an object of type String makes no difference. hth, bear
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Gabriel White
Ranch Hand
Joined: Mar 02, 2003
Posts: 233
|
|
But if you want our help, we need to see where the NPE is coming from (What method). So we need to see your code. Peace out
|
 |
Joel McNary
Bartender
Joined: Aug 20, 2001
Posts: 1815
|
|
It is true that the .equals method will throw a NPE when the object calling it is null; there is no real way around this (other than modifying the JVM somehow, as I inferr your intent from your post). However, it is possible (and even preferable) to write your own equals method outside the context of the object (i.e., make it static). E.g:
|
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Originally posted by Patrick Muls: The aString.equals(bString) method throws a NullPointerException when aString is null. While that is probably the best solution using other objects, I wonder why it is not intercepted for a String object as it is obvious that the intented result is false.
I would beg to differ. The intended result may be false in your current project, but that doesn't mean it is universally true. Also, I wonder if equals() throws a NPE if bString is null? If not, you can easily get around the NPE caused when aString is NULL by using the short-cut && operator:
|
Java API Documentation
The Java Tutorial
|
 |
Neil Laurance
Ranch Hand
Joined: Jul 18, 2002
Posts: 183
|
|
And another common code convention is to ensure string literals go first, for example: Cheers, Neil
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
Also, I wonder if equals() throws a NPE if bString is null?
bString can safely be null. The result of the comparison will be false, but no NPE will be thrown. hth, bear
|
 |
Patrick Muls
Greenhorn
Joined: Aug 08, 2001
Posts: 25
|
|
First thanks to all of you who voiced an answer to my question. Some of you asked for the code. It is part of a large webapplication written by my predecessor now almost 2 years ago, which resulted in the server getting blocked. The only message I got in the log was simply saying there was a NPE, but no trace or anything was available to find it. I didn't even know which user suffered the problem. So I spent several days trying to find it. It turned out to be in an IF statement where about a dozen strings are tested to determine they either all or none of them are filled in. The test was conform to Layne Land's suggestion, if ((s1 != null && s1.equals("")) || (s2 != null && s2.equals("") etc) But somewhere in this set, he wrote || (sx != null && sy.equals("") || (sy != null && sy.equals("") where in 99.9% of the cases sx and sy are always boh present or not. But in some records sx exists and sy not, and there the NPE was thrown. So in the future if I have to write such a test, I'll certainly will use Neil's suggestion to put the literal first. Thanks again to you all.
|
 |
 |
|
|
subject: aString;equals(bString) on null-object
|
|
|