aspose file tools*
The moose likes Java in General and the fly likes String that is null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String that is null" Watch "String that is null" New topic
Author

String that is null

ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I have a problem with String objects that were set to null. I have a String object token, and token.toString() returns null. But when I make equivalence test with (token==null) and (token.equals(null), both returned false. How can I test if a String object to be null?
Thanks!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Have you tested for an empty string that is not null? In other words, what happens when you try
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
It returns false!! Is there anyway to filter out String tokens that is null?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
If a String object is null, it can be compared to null. In your example, (token == null) will return true if String is null and token.toString() will throw a NullPointerException. What you have is a String object which has a value of "null" try token.equals("null")
Hope this helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Thanks Carl and Nick, but token.equals("null") returns false too.
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
But I used StringBuffer to take the String data, then converted it to String and then trimmed it. Now there is no null anymore.
Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
I was having the same trouble.
Found this to work.
String s;
if ( s == null )
Lou
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String that is null