4

How can I avoid NullPointerException warning in this Java code? I am working with IntelliJ and I am getting this warning:

Method Invocation 'getChartController()' may produce 'java.Lang.NullPointerException'"

chartControlButtons.add(
    new JButton("Zoom In") {{
        addActionListener(
            (ActionEvent e) -> getChartController().zoomIn()
        );
    }}
);
Akira Yamamoto
  • 4,685
  • 4
  • 42
  • 43
prre72
  • 697
  • 2
  • 12
  • 23
  • How about you just check for null getChartController() before lambda expression? Or you could check null inside it as well. – Shanu Gupta Apr 20 '18 at 06:29
  • you can add a try catch block around that statement and see what can be done in case of null pointer exception. – pkgajulapalli Apr 20 '18 at 06:31
  • @pkgajulapalli Is `null` really an exception situation here? – lexicore Apr 20 '18 at 06:34
  • 3
    Post more of your code. How does `getChartController()` look like? – lexicore Apr 20 '18 at 06:35
  • Make `getChartController()` return a [null object](https://en.m.wikipedia.org/wiki/Null_object_pattern) instead of null, such that invoking the method on it does nothing. – Andy Turner Apr 20 '18 at 06:39
  • Make `getChartController()` return a [null object](https://en.m.wikipedia.org/wiki/Null_object_pattern) instead of null, such that invoking the method on it does nothing. – Andy Turner Apr 20 '18 at 06:39
  • try to use @SuppressWarnings("ConstantConditions") – voismager Apr 20 '18 at 06:39
  • @lexicore As `getChartController()` method may return null, calling `zoomIn` method will throw a null pointer exception, right? – pkgajulapalli Apr 20 '18 at 08:20
  • @pkgajulapalli Yes. But how would we know if it can return null or not if you don't post relevant code? – lexicore Apr 20 '18 at 08:54
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Martin van Wingerden Sep 12 '19 at 05:38
  • 1
    @MartinvanWingerden That is not really a duplicate of this question. This question is asking how to fix the static analysis warning that warns the code may produce a NullPointerException. – Mark Rotteveel Sep 12 '19 at 08:34
  • @MarkRotteveel The fact is that `getChartController` might return `null` and that is the key to understanding, and an issue which is more a full duplicate (https://stackoverflow.com/questions/48858721/how-can-i-fix-this-method-invocation-isemailverified-may-produce-java-lang) was marked as a duplicate of the question mentioned above. IMHO this question has been asked in many other flavors before, its harder to choose which is the best duplicate then deciding whether its a duplicate :-) – Martin van Wingerden Sep 12 '19 at 09:53
  • 1
    @MartinvanWingerden The OP in that question is asking about the actual occurrence of a NullPointerException (they even included the stacktrace), and not just about a static analysis warning. – Mark Rotteveel Sep 12 '19 at 09:56
  • @MarkRotteveel I linked the wrong one, the one from which I started was https://stackoverflow.com/questions/57208201/cannot-resolve-method-invocation-may-produce-nullpointerexception – Martin van Wingerden Sep 12 '19 at 10:02

1 Answers1

1

You can use optional to avoid NullPointerExceptions.

Following is a kind of optional use this case.

     chartControlButtons.add(
        new JButton("Zoom In") {{
          addActionListener(
              (ActionEvent e) -> Optional.ofNullable(getChartController()).ifPresent(s -> s.zoomIn())
          );
        }}
    );
janith1024
  • 1,042
  • 3
  • 12
  • 25