Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图找到使用嵌套多对象检查NullPointerException的最佳方法-Java 8_Java_Nullpointerexception_Optional - Fatal编程技术网

试图找到使用嵌套多对象检查NullPointerException的最佳方法-Java 8

试图找到使用嵌套多对象检查NullPointerException的最佳方法-Java 8,java,nullpointerexception,optional,Java,Nullpointerexception,Optional,我正试图找到最好的方法来检查空指针异常的情况下,我已经卡住了。请支持我 这是主要课程: @Getter @Setter @AllArgsConstructor public class Person { private String name; private Car car; private Address address; } 这是汽车级别: @Setter @Getter @AllArgsConstructor public class Car {

我正试图找到最好的方法来检查空指针异常的情况下,我已经卡住了。请支持我

这是主要课程:

@Getter
@Setter
@AllArgsConstructor
public class Person {

    private String name;

    private Car car;

    private Address address;

} 
这是汽车级别:

@Setter
@Getter
@AllArgsConstructor
public class Car {

    private String name;

    private Insurance insurance;

}
这是保险类别:

@Setter
@Getter
@AllArgsConstructor
public class Insurance {

    private String name;

}
这是Address类:

@Setter
@Getter
@AllArgsConstructor
public class Address {

    private String street;

}
这是要显示的类(演示):

这是我尝试检查空指针异常的方式:

InformationDto dto = new InformationDto();
Optional.ofNullable(PERSON_CAR_NULL).map(i -> {
            dto.setPersonName(i.getName());
            Optional.ofNullable(i.getAddress()).map(e -> {
                dto.setStreetName(e.getStreet());
                return null;
            });
            return i.getCar();
        }).map(i -> {
            dto.setCarName(i.getName());
            return i.getInsurance();
        }).map(i -> {
            dto.setInsuranceName(i.getName());
            return null;
        }).orElse(null);

在我看来,一个简单的空检查将使代码更具可读性

Person person = new Person();
    InformationDto dto = new InformationDto();
    if (person != null) {
        dto.setPersonName(person.getName());
        if (person.getAddress() != null) {
            dto.setStreetName(person.getAddress().getStreet());
        }
        if (person.getCar() != null) {
            dto.setCarName(person.getCar().getName());
            if (person.getCar().getInsurance() != null) {
                dto.setInsuranceName(person.getCar().getInsurance().getName());
            }
        }
    }

if(某物不为null)
子句中设置值有什么害处?
Person person = new Person();
    InformationDto dto = new InformationDto();
    if (person != null) {
        dto.setPersonName(person.getName());
        if (person.getAddress() != null) {
            dto.setStreetName(person.getAddress().getStreet());
        }
        if (person.getCar() != null) {
            dto.setCarName(person.getCar().getName());
            if (person.getCar().getInsurance() != null) {
                dto.setInsuranceName(person.getCar().getInsurance().getName());
            }
        }
    }