有可能会报空指针异常。首先要注意的是,如果 'sfd.getAttrs()' 返回的是 null,那么 'sfd.getAttrs().getF0()' 和 'sfd.getAttrs().getF13()' 都会报空指针异常。因此,在使用它们之前,需要确保 'sfd.getAttrs()' 不为 null。可以将代码修改为如下:

if (sfd.getAttrs() != null) {
    if (sfd.getAttrs().getF0() != null && sfd.getAttrs().getF0() == 1) {
        msg1 = place.checkPlaceAttr(sfd.getAttrs());
    } else if (sfd.getAttrs().getF13() != null && sfd.getAttrs().getF13() == 1) {
        msg1 = place.checkPlaceAttr(sfd.getAttrs());
    }
}

这样可以避免空指针异常的发生。

Java 代码中避免空指针异常:if 语句最佳实践

原文地址: https://www.cveoy.top/t/topic/pgKI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录