如何在此
Java代码中对NullPointerException警告进行avid?
我正在使用IntelliJ,我收到此警告:
Method Invocation ‘getChartController()’ may produce
‘java.Lang.NullPointerException’”
chartControlButtons.add(
new JButton("Zoom In") {{
addActionListener(
(ActionEvent e) -> getChartController().zoomIn()
);
}}
);
谢谢
最佳答案 您可以使用optional来避免NullPointerExceptions.
以下是这种情况下的一种可选用法.
chartControlButtons.add(
new JButton("Zoom In") {{
addActionListener(
(ActionEvent e) -> Optional.ofNullable(getChartController()).ifPresent(s -> s.zoomIn())
);
}}
);