Uploaded image for project: 'NetBeans'
  1. NetBeans
  2. NETBEANS-5667

False positive: Dereferencing possible Null Pointer Exception

    XMLWordPrintableJSON

Details

    Description

      As posted on NETBEANS-1480, here is another clear example of the fake NullPointerException:

       

      public static void foo(Object bar) {
          Objects.requireNonNull(bar, "[bar] must not be null.");
          if (bar instanceof String) {
              System.out.println(bar.toString());
          } else {
              throw new IllegalArgumentException("[" + bar.getClass().getSimpleName() + "] not supported.");
              }
      }

      In other words, after the Objects#requireNonNull(Object, String) check, if I do an if-else with an instanceof operator, the "else" section will warn about a NullPointerException (the red method)

      Attachments

        1. prove.png
          24 kB
          Nakarukatoshi

        Activity

          People

            Unassigned Unassigned
            Nakarukatoshi Nakarukatoshi
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: