Bug 46675

Summary: [Compiler] NullPointerException with ? operator
Product: [Eclipse Project] JDT Reporter: Adam Bartoszewicz <adam.bartoszewicz>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3    
Version: 2.1.2   
Target Milestone: 2.1.3   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description Adam Bartoszewicz CLA 2003-11-14 10:58:53 EST
Bug occurs in Eclipse 2.1.2, when use ? operator with two true constant values
in boolean expression. Java mistakenly evaluate first operand expression
('s.length() == 0') and raise exception. There is no this bug in Eclipse 2.1.1
and 2.1.

To reproduce bug run this code:
--------------------------------
public class TestEclipse {
  public static void main(String[] args) {
    String s = null;
    System.out.println(
      s != null && 
      (s.length() == 0 ? TestConst.c1 : TestConst.c2)
    );
//    System.out.println(s != null && (s.length() == 0 ? true : true));

  }
  
  public static class TestConst {
    public static final boolean c1 = true;
    public static final boolean c2 = true;
  }
}
--------------------------------
Console:

java.lang.NullPointerException
	at TestEclipse.main(TestEclipse.java:6)
Exception in thread "main"
Comment 1 Philipe Mulet CLA 2003-11-18 17:48:51 EST
Reproduced.
Comment 2 Philipe Mulet CLA 2003-11-27 19:44:17 EST
Same problem with || operator:

public class TestEclipse {
  public static void main(String[] args) {
    String s = "a";
    System.out.println(
      s == null ||
      (s.length() == 0 ? TestConst.c1 : TestConst.c2)
    );
  }
  
  public static class TestConst {
    public static final boolean c1 = false;
    public static final boolean c2 = false;
  }
}
Comment 3 Philipe Mulet CLA 2003-11-28 09:05:51 EST
Fixed in latest, added regression tests:
BooleanTest#test011, test012, test013
Comment 4 Philipe Mulet CLA 2003-11-29 06:29:31 EST
Backported to 2.1 stream
Comment 5 David Audel CLA 2003-12-17 10:38:31 EST
Verified for 3.0M6
Comment 6 Frederic Fusier CLA 2004-03-01 12:36:32 EST
Verified for 2.1.3 with build M200402251535.