如何在此Java代码中提供NullPointerException警告? 我正在使用IntelliJ,我收到了这个警告:
方法调用'getChartController()'可能会生成 'java.Lang.NullPointerException'“
chartControlButtons.add(
new JButton("Zoom In") {{
addActionListener(
(ActionEvent e) -> getChartController().zoomIn()
);
}}
);
感谢
答案 0 :(得分:1)
您可以使用optional来避免NullPointerExceptions。
以下是一种可选用的案例。
chartControlButtons.add(
new JButton("Zoom In") {{
addActionListener(
(ActionEvent e) -> Optional.ofNullable(getChartController()).ifPresent(s -> s.zoomIn())
);
}}
);