Bug 46675 - [Compiler] NullPointerException with ? operator
Summary: [Compiler] NullPointerException with ? operator
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1.2   Edit
Hardware: PC Windows 2000
: P3 major (vote)
Target Milestone: 2.1.3   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-11-14 10:58 EST by Adam Bartoszewicz CLA
Modified: 2004-03-01 12:36 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.