在JAVA中,当从子类调用父类方法时抛出空指针异常。

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

Throws Nullpointer exception when calls parent class method from child class In JAVA

问题

以下是您提供的代码的翻译部分:

这是父类:

public class RequestDTO implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private String paymentProvider;
    private Boolean isEncrypt;
    
    public RequestDTO() {
    }
    
    public RequestDTO(String paymentProvider,
            Boolean isEncrypt) {
        super();
        this.paymentProvider = paymentProvider;
        this.isEncrypt = isEncrypt;
    }

    public void setPaymentProvider(String paymentProvider) {
        this.paymentProvider = paymentProvider;
    }

    public Boolean getIsEncrypt() {
        return isEncrypt;
    }

    public void setIsEncrypt(Boolean isEncrypt) {
        if (isEncrypt == null) {
            this.isEncrypt = false;
        } else {
            this.isEncrypt = isEncrypt;
        }
    }
}

以下是子类:

public class CardDetailsDTO extends RequestDTO {
    
    private static final long serialVersionUID = 1L;
    
    private String cardNumber;
    private String expiryDate;
    private String cardHolderName;
    private String cardType;
    private String cardIssueDate;
    private String cardIssueNo;

    private String modifyDummyCardNumber(String cardNum) {
        String dummyCardNumber;
        if (cardNum != null && !cardNum.isEmpty() && this.getIsEncrypt() != true) {
            dummyCardNumber = "************" + cardNum.substring(12);
            return dummyCardNumber;
        } else {
            return "********";
        }
    }

    public String getCardNumber() {
        return cardNumber;
    }
    
    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(String expiryDate) {
        this.expiryDate = expiryDate;
    }

    public String getCardHolderName() {
        return cardHolderName;
    }

    public void setCardHolderName(String cardHolderName) {
        this.cardHolderName = cardHolderName;
    }

    public String getCardType() {
        return cardType;
    }

    public void setCardType(String cardType) {
        this.cardType = cardType;
    }

    public String getCardIssueDate() {
        return cardIssueDate;
    }

    public void setCardIssueDate(String cardIssueDate) {
        this.cardIssueDate = cardIssueDate;
    }

    public String getCardIssueNo() {
        return cardIssueNo;
    }

    public void setCardIssueNo(String cardIssueNo) {
        this.cardIssueNo = cardIssueNo;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("CardDetailsDTO [expiryDate=");
        builder.append(expiryDate);
        builder.append(", cardHolderName=");
        builder.append(cardHolderName);
        builder.append(", cardType=");
        builder.append(cardType);
        builder.append(", cardIssueDate=");
        builder.append(cardIssueDate);
        builder.append(", cardIssueNo=");
        builder.append(cardIssueNo);
        builder.append(", dummyCardNumber=");
        builder.append(modifyDummyCardNumber(getCardNumber()));
        builder.append("]");
        return builder.toString();
    }
}

在子类的toString方法中调用modifyDummyCardNumber方法时,由于在modifyDummyCardNumber方法内部使用了this.getIsEncrypt(),可能导致空指针异常。您提到从父类中打印的isEncrypt值显示为"true"。要在子类中使用父类变量的值,您需要确保在子类对象中设置了正确的父类变量值。检查父类变量是否在子类对象中正确设置,以避免空指针异常。

英文:

This is Parent class

public class RequestDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String paymentProvider;
private Boolean isEncrypt;
public RequestDTO() {
}
public RequestDTO(String paymentProvider,
Boolean isEncrypt) {
super();
this.paymentProvider = paymentProvider;
this.isEncrypt = isEncrypt;
}
public void setPaymentProvider(String paymentProvider) {
this.paymentProvider = paymentProvider;
}
public Boolean getIsEncrypt() {
return isEncrypt;
}
public void setIsEncrypt(Boolean isEncrypt) {
if (isEncrypt == null) {
this.isEncrypt = false;
} else {
this.isEncrypt = isEncrypt;
}
}
}

Below is child class

   public class CardDetailsDTO extends RequestDTO {
private static final long serialVersionUID = 1L;
private String cardNumber;
private String expiryDate;
private String cardHolderName;
private String cardType;
private String cardIssueDate;
private String cardIssueNo;
private String modifyDummyCardNumber(String cardNum) {
String dummyCardNumber ;
if (cardNum != null && !cardNum.isEmpty() && this.getIsEncrypt() != true) {
dummyCardNumber = "************" + cardNum.substring(12);
return dummyCardNumber;
} else {
return "********";
}
}
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
public String getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(String expiryDate) {
this.expiryDate = expiryDate;
}
public String getCardHolderName() {
return cardHolderName;
}
public void setCardHolderName(String cardHolderName) {
this.cardHolderName = cardHolderName;
}
public String getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getCardIssueDate() {
return cardIssueDate;
}
public void setCardIssueDate(String cardIssueDate) {
this.cardIssueDate = cardIssueDate;
}
public String getCardIssueNo() {
return cardIssueNo;
}
public void setCardIssueNo(String cardIssueNo) {
this.cardIssueNo = cardIssueNo;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("CardDetailsDTO [expiryDate=");
builder.append(expiryDate);
builder.append(", cardHolderName=");
builder.append(cardHolderName);
builder.append(", cardType=");
builder.append(cardType);
builder.append(", cardIssueDate=");
builder.append(cardIssueDate);
builder.append(", cardIssueNo=");
builder.append(cardIssueNo);
builder.append(", dummyCardNumber=");
builder.append(modifyDummyCardNumber(getCardNumber()));
builder.append("]");
return builder.toString();
}
}

when I called

> modifyDummyCardNumber

method inside child class toString method it throws null pointer exception because I called this.getIsEncrypt() inside modifyDummyCardNumber method if statement as showing above ,but when I sout the isEncrypt value from the parent class it shows as "true",
How to use the parent class variable value in child class?

答案1

得分: 2

这段代码中存在一个问题:RequestDTO 类有一个无参数的构造函数,这意味着如果在使用该构造函数之前未定义该类的变量,它可能会抛出 NullPointerException(这可能是它抛出 NullPointerException 的原因)。

您还可以在类的变量声明时分配默认值。

无论如何,只要将变量 isEncrypt 赋予任何值,这段代码就不应该抛出任何 NullPointerException

英文:

There is one problem in this code: the class RequestDTO has a constructor without arguments, which means that it can throw NullPointerException if the variables of this class are not defined before using this constructor (this might be why it throws you NullPointerException).

You can also assign default values on the declaration of each variable of the class.

Anyway this code shouldn't throw any NullPointerException if the variable isEncrypt is assigned to any value.

答案2

得分: 1

由于 isEncrypt 从未被设置,因此默认为 null

术语 getIsEncrypt()! = false 试图将 null 解包为 bool,这将失败。

请执行以下操作:

public class RequestDTO implements Serializable {
    // ...
    private boolean isEncrypt;
    // ...
}

同时,在您处理这个问题时:

if (cardNum != null && !cardNum.isEmpty() && !this.getIsEncrypt()) {
英文:

Since isEncrypt is never set, it is null by default.

The term getIsEncrypt()! = false tries to unbox the null as bool, which fails.

Do

public class RequestDTO implements Serializable {
// ...
private boolean isEncrypt;
// ...
}

and, while you're at it

if (cardNum != null && !cardNum.isEmpty() && !this.getIsEncrypt()) {

答案3

得分: 0

为避免这种错误,请将isEncrypted字段的类型更改为boolean(原始类型)。

英文:

To avoid this kind of errors change the type of isEncrypted field to boolean(primitive type).

huangapple
  • 本文由 发表于 2020年10月7日 20:05:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64243657.html
匿名

发表评论

匿名网友

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

确定