Search code examples
javastringequalsjoptionpane

"null" String .equals("null") return false java (JOptionPane)


Ok sorry If I am making a duplicate, but i really looked through the site and did not seem to find something similar to my problem. Or at least it is working for other people but not for me.

My main problem is that I use a JOptionPane, and I store the result in a String. When the close button is clicked, the value stored in the string is "null" as I noticed it in the console.

String addedItemName = (String)addItemPane.showInputDialog(null,"Select item to add to basket","Add Item",JOptionPane.QUESTION_MESSAGE,null,items,"Select Item");
System.out.println("added item : " + addedItemName);

gets me this result in console :

added item : null

Therefore, as I am looking to know when the close button was clicked by comparing my addedItemName String to a "null" String. Which seems to never work, and quite frustrates me because I don't really get it. Here are a few other tests I made in order to show how every different way I try it but it still doesn't work.

        String nullString = "null";
                      
        boolean test1 = Objects.equals(addedItemName, nullString);
        boolean test2 = Objects.equals(nullString,addedItemName);
        boolean test3 = nullString.equals(addedItemName);
        boolean test4 = nullString.equalsIgnoreCase(addedItemName);

        System.out.println(addedItemName);
        System.out.println(nullString);

        System.out.println(test1);
        System.out.println(test2);
        System.out.println(test3);
        System.out.println(test4);

With following console output when i click the close button :

null

null

false

false

false

false

So Basically, console shows me that the addedItemName is "null" when the close button is clicked, but when I compare it to a "null" String, it somehow never is equal.

Thanks for anyone that will show me how stupid I actually am and why it doesn't work for a goofy simple reason.

(also little issue but don't know if it's important : those lines of equality testing return nullPointerException error even if the strings get the value)

   boolean test1 = addedItemName.equals(nullString);
   boolean test2 = addedItemName.equalsIgnoreCase(nullString);

   System.out.println(addedItemName);
   System.out.println(nullString);

output :

null

null

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException


Solution

  • The null-String must be:

    String nullString = null;
    

    cause 'null' means a not referenced object (keep in mind everything in java is a object)

    String nullString = "null";
    

    is a valid reference to a String which contains the letters null Your output is a little bit obfuscating, cause

    System.out.println("added item : " + null);
    

    results in the output you saw. Thats cause the String concatination:

    https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.11

    ...Now only reference values need to be considered. If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.