How do I avoid null pitfalls when comparing Strings?
Jason Allen
Greenhorn
Joined: Jan 25, 2001
Posts: 26
posted
0
I'm having difficulty coming up with a good solution for comparing string values and avoiding the NullPointerException. Here's what I'm trying to do with half-way decent looking code I'll give the psuedocode: Compare two String values to determine if they are equal. String oldvalue; String newvalue; if oldvalue == newvalue then update = false What is a pretty way to write this in Java. Both oldvalue and newvalue can have null. In VB, all I have to say is if oldvalue = newvalue then do whatever. In Java, it seems as if it is comparing at the address level when you use ==. If I use oldvalue.compareTo(newvalue) I get NullPointerException. If I use oldvalue == newvalue i don't believe it is ever "equal" because it possibly comparing addresses... If I were checking for a particular constant, I could just do "Joe".equals(name). But either argument could be null in my case. Any ideas would be appreciated, Thanks, Jason.
Charles Mullins
Greenhorn
Joined: Dec 22, 2000
Posts: 17
posted
0
Have you considered using a try/catch block? try{ if (oldvalue == newvalue){ update = false; } } catch (NullPointerException e){ System.out.println("variable is null"); }
Originally posted by Jason Allen: I'm having difficulty coming up with a good solution for comparing string values and avoiding the NullPointerException. Here's what I'm trying to do with half-way decent looking code I'll give the psuedocode: Compare two String values to determine if they are equal. String oldvalue; String newvalue; if oldvalue == newvalue then update = false What is a pretty way to write this in Java. Both oldvalue and newvalue can have null. In VB, all I have to say is if oldvalue = newvalue then do whatever. In Java, it seems as if it is comparing at the address level when you use ==. If I use oldvalue.compareTo(newvalue) I get NullPointerException. If I use oldvalue == newvalue i don't believe it is ever "equal" because it possibly comparing addresses... If I were checking for a particular constant, I could just do "Joe".equals(name). But either argument could be null in my case. Any ideas would be appreciated, Thanks, Jason.
Bosun Bello
Ranch Hand
Joined: Nov 06, 2000
Posts: 1506
posted
0
Why don't you check if any of the values/variables are false? If any is then don't do the comparison. If none of them is null, use the eqauls() method to do your comparison. Good luck, Bosun
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Pat Barrett
Ranch Hand
Joined: Jan 03, 2001
Posts: 63
posted
0
The way to compare String object contents is to use the equals() method. This would return true if the String contents were EXACTLY equal. For example;
the "do something" code block would be executed. hope this is what you were looking for... Pat B.
Ben Roy
Ranch Hand
Joined: Nov 01, 2000
Posts: 70
posted
0
Just thought I'd throw in one other thought. I was running into lots of these same types of situations, or a similar one where I wanted to do myVar.equals("MyValue") and I had the same thing. If it was null it would throw and exception. For this type of thing I switched it around and did it like "MyValue".equals(myVar), which won't throw an exception if myVar is null. I know that doesn't completely address your particular issue. For your problem, you might want to consider writing a function to handle your comparison, particularly if you do this often in your code. You could go with the try/catch or a couple of if/then statements to check for null before doing the comparison inside the function.
Peter Tran
Bartender
Joined: Jan 02, 2001
Posts: 783
posted
0
Assuming your String reference variables are strObj1 and strObj2, you could do the following:
-Peter
Jason Allen
Greenhorn
Joined: Jan 25, 2001
Posts: 26
posted
0
Ok, has something changed with the == operator for strings in v1.3? I was almost positive that I couldn't use strObj1 == strObj2 in v1.1.8 and have it evaluate correctly when the arguments were equal. Now I try it in v1.3 and it seems to work...so I can just say strObj1 != strObj2 with no problems. Let me know if I have missed something. Jason.
Charles Mullins
Greenhorn
Joined: Dec 22, 2000
Posts: 17
posted
0
As long as the Strings are pointing to the same object then == will work just fine. But if they are pointing to a different object then == will return false even if the values match. equals will compare the String contents if they are the same object or different. Hopefully that makes sense...