尝试找到在Java 8中检查嵌套多个对象的空指针异常的最佳方法。

huangapple go评论81阅读模式
英文:

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());
			}
		}
	}

huangapple
  • 本文由 发表于 2020年8月24日 23:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63563724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定