Olindholm
Olindholm

Reputation: 390

if(String != null) still passes?

Sometimes, or well, pretty often i need to see if a String is null or isn't, so i use a conditional like this:

if(str != null) {
}

It worked fine until this day, now even strings that are null pass for some reason which I don't get. Does anyone that knows an explanation? or a solution?

Edit: OK, so for some reason the string doesn't seem to be null, which is very odd since i never defined what it was, only denfined that it existed. So instead the string was a string of the word "null". Which i have totally no idea why it would be? When i do following:

String str;

then it's automatically null, right? And either way if it's not why would it become the string "null"?

Upvotes: 2

Views: 6164

Answers (7)

Matt Stephenson
Matt Stephenson

Reputation: 8620

And either way if it's not why would it become the string "null"?

If you come across a String with the value "null" (the 4 character word, not a null reference), then you or someone has set that string to "n"-"u"-"l"-"l". It's possible you did it without meaning to. If you meant this:

str = null

You may have typed this:

str = "null"

Compile and run this test program, then take out the quotes on the str = "null" line. Compile and run again - you'll see the different behavior.

public class shortfile {
    public static void main(String[] argv) {
        String str;
        str = "null";
        if (str == null) {
            System.out.println("str is really a null reference");
        }
        else {
            int len = str.length();
            System.out.println("We set str to the literal word null");
            System.out.println("str's length is " + len);
        }
    }
}

Upvotes: 1

Adriaan Koster
Adriaan Koster

Reputation: 16209

A possible cause is to leave out the curly braces altogether in a multiline conditional block:

if(str != null)
    callSomeMethod();
    System.out.println("length=" + str.length());

In this classy (and classic) example System.out.println will always be executed since it is equivalent to:

if(str != null) {
    callSomeMethod();
}
System.out.println("length=" + str.length());

--

Just thought of another possible pitfall:

if(str != null) {
    System.out.println("str=" + str);
}

If this prints:

str=null

then the value of str might be "null" (the four-character word "null").

Upvotes: 1

bchetty
bchetty

Reputation: 2241

That's probably because of empty strings or strings with whitespace characters. Use the below code:

if(str != null && !str.trim().isEmpty()) {
}

EDIT: Sorry for misreading and submitting a fast solution, before I rushed up to work.

Maybe the null check fails because of how 'null' is interpreted by different programming languages or libraries. Below are some interesting links with good discussion:

1) Stackoverflow - What is null in Java?

2) Stackoverflow - Where is null in memory?

3) Stackoverlfow - Null \u0000 in Java String?

Upvotes: 0

aviad
aviad

Reputation: 8278

Sorry for off-topic, but I remember once I got into similar situation because of a ';' (semicolon) that sneaked into the end of the line of the if condition. It got me really crazy because no matter how I changed the condition the following block of code always executed...I found the problem after an hour when I copied and pasted the block of code into text editor.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500695

The explanation is that your diagnosis is incorrect. If str is null, the body of that if statement will not be executed - it's as simple as that. You haven't said anything about how you've diagnosed that str is null but that code is still being executed - if it's because you're seeing a NullPointerException, there are various reasons you might still be getting that. In particular, it could well be due to a different expression being dereferenced when null within the if block.

Alternatively, is str a non-local variable being modified by other threads, by any chance?

EDIT: Note that I've been assuming you know the difference between null and a reference to an empty string - you really are talking about the value of str being null, right? If you're talking about empty strings, that's a very different matter.

Upvotes: 9

dhakim
dhakim

Reputation: 508

Based on your null being lower case, I assume you're using java or C#.

The classiest way for an if statement to be executed when its obviously false is for you to accidentally put a semicolon on the end. Like this

if (false);
{
    System.out.println("How can this be happening!?");
}

I recommend looking very carefully at your if statements, and putting printlns in both the if and else clauses as necessary. I guarantee you, if (str != null) will definitely not run when str is null.

Upvotes: 1

Dumbo
Dumbo

Reputation: 14112

I would check for length as well after checking for null:

if(stringVar == null || stringVar.length() == 0) { /* DO STUFF */ }

To make life easier you can make a extension method:

public static boolean notEmpty(String s) 
{ 
return (s != null && s.length() > 0);
}

Upvotes: 1

Related Questions