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

Generic types can cause NullPointerException in Java

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      macOS Monterey version 12.0.1

      Java:

      openjdk version "17.0.1" 2021-10-19
      OpenJDK Runtime Environment Temurin-17.0.1+12 (build 17.0.1+12)
      OpenJDK 64-Bit Server VM Temurin-17.0.1+12 (build 17.0.1+12, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      Was able to trigger a NullPointerException in javac:

      [INFO] Compiling 1 source file to /Users/apiburn/Source/jdk-error/target/test-classes
      An exception has occurred in the compiler (17.0.1). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
      java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Type.getThrownTypes()" because "tree.meth.type" is null
      at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitApply(Flow.java:1439)
      at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1797)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
      at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
              ...



      REGRESSION : Last worked in version 8u311

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See example maven project: https://github.com/mtday/jdk-error

      The code in question uses Jakarta.ws.rs.core classes (Response and GenericType):

      Assertions.assertEquals(lists, response.readEntity(new GenericType<>() {}));

      This usage of the GenericType class without specifying the parameterized type causes the NPE in javac.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected the compiler to indicate an error in the source code, or to successfully determine the type at compile time and successfully compile the code.
      ACTUAL -
      NullPointerException thrown by the javac compiler. Full stack trace shown in the example project: https://github.com/mtday/jdk-error

      ---------- BEGIN SOURCE ----------
      https://github.com/mtday/jdk-error
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Two workarounds:

      Instead of this line that causes the error:

      Assertions.assertEquals(lists, response.readEntity(new GenericType<>() {}));

      Use this line:

      Assertions.assertEquals(lists, response.readEntity(new GenericType<List<TodoList>>() {}));

      Or these two lines:

      List<TodoList> results = response.readEntity(new GenericType<>() {});
      Assertions.assertEquals(lists, results);

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              adev ANUPAM DEV
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: