英文:
Trying to find the best way to check NullPointerException with nested multiple Object - Java 8
问题
以下是已经翻译好的内容:
我正在尝试找到在我陷入困境的情况下检查空指针异常的最佳方法。请支持我。
这是主要的类:
@Getter
@Setter
@AllArgsConstructor
public class Person {
    private String name;
    private Car car;
    private Address address;
} 
这是Car类:
@Setter
@Getter
@AllArgsConstructor
public class Car {
    private String name;
    private Insurance insurance;
}
这是Insurance类:
@Setter
@Getter
@AllArgsConstructor
public class Insurance {
    private String name;
}
这是Address类:
@Setter
@Getter
@AllArgsConstructor
public class Address {
    private String street;
}
这是显示信息的类(演示):
@Setter
@Getter
public class InformationDto {
    private String personName;
    private String carName;
    private String insuranceName;
    private String streetName;
}
这是我尝试检查空指针异常的方式:
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_CAR_NULL)在提供的文本中并未提供定义,因此我保留了原始文本以供参考。
英文:
I am trying to find the best way to check null pointer exception with the situation that I have got stuck. Please support me.
This is the main Class:
@Getter
@Setter
@AllArgsConstructor
public class Person {
    private String name;
    private Car car;
    private Address address;
} 
This is Car class:
@Setter
@Getter
@AllArgsConstructor
public class Car {
    private String name;
    private Insurance insurance;
}
This is the Insurance class:
@Setter
@Getter
@AllArgsConstructor
public class Insurance {
    private String name;
}
This is the Address class:
@Setter
@Getter
@AllArgsConstructor
public class Address {
    private String street;
}
This is the Class to Display (demo):
@Setter
@Getter
public class InformationDto {
    private String personName;
    private String carName;
    private String insuranceName;
    private String streetName;
}
This is the way that I try to check null pointer exception:
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);
答案1
得分: 1
在我看来,一个简单的空值检查会使代码更易读。
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());
        }
    }
}
英文:
IMO, A simple null check will make the code more readable.
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());
			}
		}
	}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论