intellij s == null returns false on un initialized variable s

the variable s is declared but not initialized

during debug the  s == null returns false

if i run the same app it returns true

so, intellij returns a different result for an uninitialized null check on a variable depending on
whether you run it or debug it

anyone else see this?

6 comments
Comment actions Permalink

Please provide sample code to reproduce.

0
Comment actions Permalink

class Simple {
  String s;
  public Simple(String si) { s = si; }
  public String toString() { return s; }
  public void setString(String sNew) { s = sNew; }
}

class Second {
  Simple simple;
  String s;
  public Second(String si) {
    s = si; // 'simple' not initialized
  
}
  public void check() {

    if((simple) == null )
      System.out.println("not initialized");
    else
      
System.out.println("initialized");
  }
  public Simple lazy() {
    if(simple == null) {
      System.out.println("Creating Simples");
      simple = new Simple(s);
    }
    return simple;
  }
  public Simple getSimple() { return lazy(); }

  public String toString() {
    return lazy().toString();
  }
  public void setSimple(String sNew) {
    lazy().setString(sNew);
  }
}

public class E01_Composition {

  public  E01_Composition (){


  }
  public static void main(String args[]) {
    Second second = new Second("Init String");
    second.check();

  }
}
0
Comment actions Permalink

when I run main with debug i get initialized in console even though i know it is not initialized

walk through the code and you will wonder why it thinks simple is not null inside check()

when i run the same without debug i get the result i expect to get of not initialized

the outcome is that i get 2 different answers

the run sais it is not initialized

the debug sais that it is

the correct result is the run version

other ide's give the correct answer from the run so i thought it was helpful to submit

0
Comment actions Permalink

i am using build 141.177 March 23, 2015

0
Comment actions Permalink

It's expected behavior of running under debugger when you have enabled toString object view, disable it here:

https://dl.dropboxusercontent.com/u/2752840/ShareXmod/2015-04/Settings_2015-04-03_16-24-00.png

What happens is that debugger calls toString that initializes your object.

0
Comment actions Permalink

Perfect!

That's it - Fixed!

0

Please sign in to leave a comment.