File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String that is null

 
ricky gonzalez
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Have you tested for an empty string that is not null? In other words, what happens when you try
 
ricky gonzalez
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns false!! Is there anyway to filter out String tokens that is null?
 
Carl Trusiak
Sheriff
Posts: 3340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ricky gonzalez
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Carl and Nick, but token.equals("null") returns false too.
 
ricky gonzalez
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic