为以下代码加非空判断: PortMonitor pm = new PortMonitor(); List<PortMonitor> portMonitorList = portMonitorService.selectPortMonitorList(pm); for (PortMonitor portMonitor: portMonitorList) { String ip = portMonitor.getIp(); Integer port = portMonitor.getPort(); boolean portStatus = portMonitorService.isPortOpen(ip, port); portMonitor.setStatus(portStatus ? "0" : "1"); } portMonitorService.batchUpdateStatus(portMonitorList);
时间: 2024-03-13 21:46:28 浏览: 25
好的,为了加非空判断,可以在使用 `portMonitorService.selectPortMonitorList(pm)` 方法前,先判断 `portMonitorService` 是否为 null,并且在使用 `portMonitor.getIp()` 和 `portMonitor.getPort()` 方法前,先判断 `portMonitor` 是否为 null。具体修改如下:
```
if(portMonitorService != null) {
PortMonitor pm = new PortMonitor();
List<PortMonitor> portMonitorList = portMonitorService.selectPortMonitorList(pm);
if(portMonitorList != null) {
for (PortMonitor portMonitor: portMonitorList) {
if(portMonitor != null) {
String ip = portMonitor.getIp();
Integer port = portMonitor.getPort();
boolean portStatus = portMonitorService.isPortOpen(ip, port);
portMonitor.setStatus(portStatus ? "0" : "1");
}
}
portMonitorService.batchUpdateStatus(portMonitorList);
}
}
```
这样,在代码执行前会先进行非空判断,避免出现空指针异常。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)