Alex Rivera | Logout

How to check a string against null in java?

Asked 2010-05-27T11:11:23.943
108

How can I check a string against null in java? I am using

stringname.equalsignorecase(null)

but it's not working.

Edit
Report

1 Answer

3

I'm not sure what was wrong with The MYYN's answer.

if (yourString != null) {
  //do fun stuff with yourString here
}

The above null check is quite alright.

If you are trying to check if a String reference is equal (ignoring case) to another string that you know is not a null reference, then do something like this:

String x = "this is not a null reference"
if (x.equalsIgnoreCase(yourStringReferenceThatMightBeNull) ) {
  //do fun stuff
}

If there is any doubt as to whether or not you have null references for both Strings you are comparing, you'll need to check for a null reference on at least one of them to avoid the possibility of a NullPointerException.

answered 2010-05-27T11:19:46.030

Your Answer