Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8267559

[lworld] [lw3] NPE thrown when attempting to write null to a null-free array has incorrect error message

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • None
    • repo-valhalla
    • hotspot

      The following code:


      public class NullFreeArray {
          static primitive class Point {
      int x = 0, y = 0;
          }

          static void setNull(Object[] a) {
      try {
      a[0] = null;
      } catch(Throwable t) {
      System.out.println(t);
      }
          }
          
          public static void main(String[] args) {
      Point[] a = new Point[10];
      setNull(a);
          }
      }


      produces this output:

      java.lang.NullPointerException: Cannot store to object array because "<parameter1>" is null

      However, <parameter1> in method setNull() is not null, the NPE is caused by the null reference that the code tries to write to the null-free Point array.

            fparain Frederic Parain
            fparain Frederic Parain
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: