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

NullPointerException accessing outer class variable

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.5, 1.1.6, 1.2.0
    • tools
    • x86, sparc
    • solaris_2.6, windows_nt



      Name: bk70084 Date: 07/20/98


      Try running the following code. The bang() function
      in bar2 will cause a NullPointerException, even though the
      access is completely correct. I would guess it has something to
      do with the subclassing from bar1, which is not an inner class


      class bar1 {
          bar1() {
              bang();
          }

          void bang() {
          }
      }

      class foo {
          String i = "hi";

          class bar2 extends bar1 {
      bar2() {
      super();
      }

      void bang() {
                  System.out.println("outer instance is "+foo.this); // will print null
      System.out.println("i is "+i); // will throw NullPointerException
      }
          }

          foo() {
      new bar2();
          }

          public static void main(String argv[]) {
              new foo();
          }
      }
      (Review ID: 29780)
      ======================================================================

            dstoutamsunw David Stoutamire (Inactive)
            bklocksunw Brian Klock (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: