-3

How to detect if my string text does Not equal "null"? I have tried this, but it doesn't work. It always executes the rest of code and displays the dialog-

if (!text.equals("null")) {
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("BROADCAST").setMessage(text).create();
        alertDialog.getWindow().setType(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();
    } else {
        // don't show
    }

PS: I AM NOT TRYING TO CHECK IF MY STRING IS NOT NULL. I SAID "NULL" WHICH IS A TEXT STORED IN MY STRING

joey
  • 11
  • 1
  • 1
  • 5
  • http://stackoverflow.com/questions/2601978/how-to-check-if-my-string-is-equal-to-null – WOUNDEDStevenJones Mar 01 '17 at 23:28
  • 2
    Possible duplicate of [How to check if my string is equal to null?](http://stackoverflow.com/questions/2601978/how-to-check-if-my-string-is-equal-to-null) – Ken White Mar 01 '17 at 23:29
  • Your if statement is checking if text is equal to the string "null". If you want to check if the String object is null or not null, look at @MichaelVescovo 's first example. – JoryAnderson Mar 01 '17 at 23:31
  • Do you want to check if your text is empty or has the string "null" ? Your code seems correct for later case. – Sulav Timsina Mar 01 '17 at 23:31
  • @KenWhite **bro do you understand English!?** – joey Mar 01 '17 at 23:53
  • 1
    Do you understand **manners**? The **PS** wasn't there when I posted the link, and others also thought you meant something else as well (including the person who wrote an entire answer based on the same thing). It would have been more clear if you said **the string "null"**, so we knew it wasn't just a beginner's mistake. And please be polite here. If you can't do that, feel free to go somewhere else to ask for **free help** solving **your problem**. Don't blame me because a user with 1 rep and out of context code seemed to have made a rookie mistake. Write clearer questions. – Ken White Mar 02 '17 at 00:00
  • @KenWhite bro It doesn't need "the PS" to understand a simple question. I put it there only for you. The question was correct without PS and still is. – joey Mar 02 '17 at 00:02

2 Answers2

3

You can use:

if(text != null) {
    AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setTitle("BROADCAST").setMessage(text).create();
    alertDialog.getWindow().setType(
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alertDialog.show();
}

Or to check empty string:

if(!text.equals("") {
    AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setTitle("BROADCAST").setMessage(text).create();
    alertDialog.getWindow().setType(
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alertDialog.show();
}

Or both:

if(text != null && !text.equals("") {
    AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setTitle("BROADCAST").setMessage(text).create();
    alertDialog.getWindow().setType(
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alertDialog.show();
}

Edit: if you're trying to check if the string is equal to the word "null" then your original code looks fine. I would suggest using the debugger or printing some log statements to check what's going on.

So:

private static final String TAG = "SomeActivity";

if (!text.equals("null")) {
    // Not equal to the word null
    Log.d(TAG, "text value should NOT be null and is: " + text);

    //AlertDialog alertDialog = new AlertDialog.Builder(this)
    //        .setTitle("BROADCAST").setMessage(text).create();
    // alertDialog.getWindow().setType(
    //        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    // alertDialog.show();
} else {
    // don't show
    Log.d(TAG, "text value should be null and is: " + text);
}

But really this is just testing that you've not made a mistake. Further to this, I would recommend not using "null" to indicate the empty string. It would likely confuse anyone else looking at the code. Stick with "" which is more standard.

Michael Vescovo
  • 3,741
  • 4
  • 32
  • 45
1

As-is, your code is currently checking if the string is equal to the string "null". If you want to see if the String object itself is null or does not exist, consider:

if (text != null) {
    AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setTitle("BROADCAST").setMessage(text).create();
    alertDialog.getWindow().setType(
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alertDialog.show();
} else {
    // don't show
}
JoryAnderson
  • 103
  • 7
  • 1
    That won't compile. You can't do `if (text)` because `text` is a String and you need a boolean value for the guard. @MichaelVescovo's answer with `if(text !=null)` works. – Fodder Mar 01 '17 at 23:32
  • @Fodder Thank you! I've been programming in Python a lot recently. I wasn't sure if it would compile or not in Java. I've edited my answer. – JoryAnderson Mar 01 '17 at 23:34
  • @Arcaeic check the bold text in my question – joey Mar 01 '17 at 23:41
  • @joey Well in that case the code you presented should work. We don't know how text is assigned the value "null" in the first place, which makes it difficult to troubleshoot your problem. Before you go into the if statement, do a `System.out.println(text);` and check if the value of text is what you want. – JoryAnderson Mar 01 '17 at 23:48
  • ill try it with toast – joey Mar 01 '17 at 23:52