0

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?

dasitha
  • 331
  • 2
  • 5
  • 15
  • Your class CardDetailsDTO is not extending the parent class RequestDTO ? – Arv Oct 07 '20 at 12:11
  • Use `super.getIsEncrypt()` instead of `this.getIsEncrypt()` – flyingfox Oct 07 '20 at 12:13
  • No.. It extends . Look again "public class CardDetailsDTO extends RequestDTO" – dasitha Oct 07 '20 at 12:13
  • @lucumt will try. – dasitha Oct 07 '20 at 12:14
  • the call to the method looks correct, can you show us how you use `modifyDummyCardNumber` and where you set `isEncrypt`? Also i think you need brackets around `this.getIsEncrypt() != true`, since i think he will work from left to right, so it compares everything else first and then looks if it is not false. But i am not 100% sure about that. – Tobias Oct 07 '20 at 12:17
  • @user11670196 I called method inside the child class toString method, Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("CardDetailsDTO builder.append(", dummyCardNumber="); builder.append(modifyDummyCardNumber(getCardNumber())); builder.append("]"); return builder.toString(); } – dasitha Oct 07 '20 at 12:23
  • @lucumt did not work with super also – dasitha Oct 07 '20 at 12:26
  • because `isEncrypt` is private in the RequestDTO class, you'd need a getter for it if you plan on using it in the subclasses. Or making it `protected` – Shark Oct 07 '20 at 12:31
  • Please provide a stack trace and a code sample calling the method where the error occurs. Also, why are you using `Boolean ` instead of `bool`, and why not `if (! getIsEncrypt() `? – daniu Oct 07 '20 at 12:34
  • @Shark already im calling the getter method for isEncrypt. "this.getIsEncrypt() != true" – dasitha Oct 07 '20 at 12:34
  • 1
    But on a second look, nothing is really setting the `isEncrypt` to anything, and due to it being `Boolean` it starts off as `null`. the child class should invoke the `setIsEncrypt` method in it's constructor most probably. – Shark Oct 07 '20 at 12:35
  • @dasitha that getter will just return `null` due to it never being set to anything. (at least "nowhere from the code you posted") – Shark Oct 07 '20 at 12:36
  • @daniu im calling the method inside the child class toString method, error is "java.lang.NullPointerException upa_tomcat | at com.noetic.dto.elavon.CardDetailsDTO.modifyDummyCardNumber(CardDetailsDTO.java:28) " – dasitha Oct 07 '20 at 12:36
  • @Shark im initializing the DTO via rest end point , when I sout the requestDTO it shows isEncrypt value as true. – dasitha Oct 07 '20 at 12:38
  • 2
    I tested it on my pc and it works fine, as long as as i set isEncrypted before the call. That is once again why i wanted you show us the rest of the code, especially the part where you set isEncrypted – Tobias Oct 07 '20 at 12:48

3 Answers3

2

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.

Mario Codes
  • 689
  • 8
  • 15
DaniiLuna
  • 86
  • 3
  • when I remove "&& this.getIsEncrypt() != true" from dummycardnumber, the code execute successfully. will try without default constructor . – dasitha Oct 07 '20 at 12:49
  • "the method modifyDummyCardNumber is private and it is not executed by CardDetailsDTO" - yes it is? – daniu Oct 07 '20 at 18:01
1

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()) {
daniu
  • 14,137
  • 4
  • 32
  • 53
  • I am not sure if isEncrypt is never set, since he says it is true if he prints it. Also he did not show us the part of the code that is using the class yet. – Tobias Oct 07 '20 at 13:53
  • @user11670196 Yes, it's a prime example why [mcve] is needed. – daniu Oct 07 '20 at 13:53
0

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

Alexandr Ivanov
  • 389
  • 1
  • 5
  • 7