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

DiagnosticListener will fail to handle NullPointerException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 6
    • tools
    • generic
    • generic

      DiagnosticListener will fail to handle the Null objects, Compiler API will fail when trying to access the object which is not initialized inside a DiagnosticListener.

      Please see the following code
      <code>
      import javax.tools.*;

      public class Foo {
          private StringBuilder sb;
          public void test(String... args) {
              JavaCompilerTool javac = ToolProvider.defaultJavaCompiler();
              DiagnosticListener dl = new DiagnosticListener() {
                  public void report(DiagnosticMessage message) {
                      sb.append("test");
                  }
              };
              javac.run((JavaFileManager)null, dl, null, args);
          }
          public static void main(String... arg) {
              Foo test = new Foo();
              test.test("Test6.java");
          }
      }
      </code>
      In the above code, sb is not initialized and trying to access inside DiagnosticListener.

      Output of the code when compiled and run
      <Output>
      bash-3.00$ javac -Xlint Foo.java
      bash-3.00$ java Foo
      An exception has occurred in the compiler (1.6.0-ea). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
      java.lang.NullPointerException
              at Foo$1.report(Foo.java:9)
              at com.sun.tools.javac.util.Log.writeDiagnostic(Log.java:444)
              at com.sun.tools.javac.util.Log.report(Log.java:432)
              at com.sun.tools.javac.util.Log.error(Log.java:363)
              at com.sun.tools.javac.comp.Resolve$ResolveError.report(Resolve.java:1576)
              at com.sun.tools.javac.comp.Resolve.access(Resolve.java:1053)
              at com.sun.tools.javac.comp.Resolve.access(Resolve.java:1073)
              at com.sun.tools.javac.comp.Resolve.resolveIdent(Resolve.java:1136)
              at com.sun.tools.javac.comp.Attr.visitIdent(Attr.java:1625)
              at com.sun.tools.javac.tree.Tree$Ident.accept(Tree.java:1251)
              at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:277)
              at com.sun.tools.javac.comp.Attr.attribType(Attr.java:305)
              at com.sun.tools.javac.comp.MemberEnter.visitVarDef(MemberEnter.java:604)
              at com.sun.tools.javac.tree.Tree$VarDef.accept(Tree.java:592)
              at com.sun.tools.javac.comp.MemberEnter.memberEnter(MemberEnter.java:385)
              at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:556)
              at com.sun.tools.javac.tree.Tree$VarDef.accept(Tree.java:592)
              at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:277)
              at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:312)
              at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:328)
              at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:626)
              at com.sun.tools.javac.tree.Tree$Block.accept(Tree.java:630)
              at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:277)
              at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:312)
              at com.sun.tools.javac.comp.Attr.visitTry(Attr.java:792)
              at com.sun.tools.javac.tree.Tree$Try.accept(Tree.java:808)
              at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:277)
              at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:312)
              at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:328)
              at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:626)
              at com.sun.tools.javac.tree.Tree$Block.accept(Tree.java:630)
              at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:277)
              at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:312)
              at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:537)
              at com.sun.tools.javac.tree.Tree$MethodDef.accept(Tree.java:545)
              at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:277)
              at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:312)
              at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:2525)
              at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:2457)
              at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:2393)
              at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:636)
              at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:549)
              at com.sun.tools.javac.main.Main.compile(Main.java:680)
              at com.sun.tools.javac.main.Main.compile(Main.java:611)
              at com.sun.tools.javac.api.Tool.runImpl(Tool.java:122)
              at com.sun.tools.javac.api.Tool.run(Tool.java:131)
              at Foo.test(Foo.java:12)
              at Foo.main(Foo.java:16)

      </Output>

            jjg Jonathan Gibbons
            savadhansunw Seetharama Avadhanam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: