Am not able to see your Use case here- You want to check if the value is not null? Then you could use- <variable> != null, or to check for empty string you could try- <variable>.equals("").
mohammed sanaullah wrote:Am not able to see your Use case here- You want to check if the value is not null? Then you could use- <variable> != null, or to check for empty string you could try- <variable>.equals("").
But remember that an empty String ("") is not the same as null.
Jan Hoppmann wrote:But remember that an empty String ("") is not the same as null.
This sometimes confuses people. A good way to think of the difference is an empty string is a box with nothing in it. A null string means you don't even have a box.
fred rosenberger wrote:
This sometimes confuses people. A good way to think of the difference is an empty string is a box with nothing in it. A null string means you don't even have a box.
Why are you using such a test in a Listener? That sounds to me like very non-object-oriented programming. You have probably got some poor design there. You can avoid problems with null Strings very easily, by swapping the order of operands, like this:That test cannot suffer a NullPointerException even if user is null.
There are a few instances where a formal null check is required: for example in the old-fashioned use of a FileReader. Note I am showing the correct way to do it with a finally block. Look at this post, and the post farther down where I realised I had made a mistake. That will show you how to check for nullity.