Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 420154

Summary: [null] Wrong report of Potential NullPointerAccess
Product: [Eclipse Project] JDT Reporter: kurt <kurt2002>
Component: CoreAssignee: Stephan Herrmann <stephan.herrmann>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: kurt2002, srikanth_sankaran
Version: 4.3.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard: stalebug

Description kurt CLA 2013-10-23 06:23:07 EDT
Following three examples, where a worng PNPA is reported.

  static void potentialNPAccess_1() {
    final Vector<Component> vo = new Vector<Component>();
    // --- wrong PNPA only occurs if variable declared here AND set to null
    Component c = null;
    for (final Enumeration<Component> e = vo.elements(); e.hasMoreElements();) {
      // --- no PNPA if declared like this
      // final Component c = e.nextElement();
      c = e.nextElement();
      // --- "||" neccessary to have the wrong PNPA !
      if ((c instanceof JTextField) || (c instanceof JPasswordField))
        c.setEnabled(true); // XXX wrong PNPA on c
    }
  }

  static void potentialNPAccess_2(boolean b1, boolean b2, final Object o1,
      final Object o2) {
    final Iterator<Object> it = new Vector<Object>().iterator();
    // --- wrong PNPA only occurs if variable declared here AND set to null
    Object ox = null;
    do {
      // --- no PNPA if declared like this
      // final Object ox = it.next();
      ox = it.next();
      if (b1 && ox.equals(o1))
        b1 = false;
      if (b2 && ox.equals(o2)) // XXX wrong PNPA on ox
        b2 = false;
    } while (it.hasNext() && (b2 || b1));
  }

  static void potentialNPAccess_3(final boolean b1) {
    final Vector<Component> vo = new Vector<Component>();
    // --- wrong PNPA only occurs if variable declared here AND set to null
    Object o = null;
    for (int i = 0; i < vo.size(); i++) {
      // --- no PNPA if declared like this
      // final Object o = vo.get(i);
      o = vo.get(i);
      // --- uncomment next line and the wrong PNPA disappears
      // final boolean b3 = o.equals("x") || b1;
      final boolean b2 = b1 || o.equals("x");
      if (b2 && (o.hashCode() == 1)) // wrong PNPA on o
        System.out.println("x and hc=1");
    }
  }
Comment 1 kurt CLA 2014-10-02 05:49:00 EDT
For your information - bug is still present in Eclipse 4.5M2:

  Version: Mars (4.5)
  Build id: I20140918-0330
Comment 2 Eclipse Genie CLA 2019-08-16 09:08:30 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.