0

I am new in coding. Currently, working on a nullpointerexception challenge on Apex.

Error: WarrantySummary: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.WarrantySummary: line 14, column 1

Code:

Integer warrantyDays; {

    if (myCase.Product_Total_Warranty_Days__c == null) {

    //below line is 14
            warrantyDays        = myCase.Product_Total_Warranty_Days__c.intValue() + 5;

    } else {

        warrantyDays        = myCase.Product_Total_Warranty_Days__c.intValue();
    }

    System.debug(warrantyDays);

   }

Thank you!

3
  • warrantyDays = myCase.Product_Total_Warranty_Days__c == null ? 5 : myCase.Product_Total_Warranty_Days__c.intValue(); is the one line solution
    – cropredy
    Jul 4 '19 at 18:52
  • thank you @cropready. why did you use question mark?
    – Batir
    Jul 8 '19 at 11:05
  • It is the syntax of a ternary. A convenient way to reduce a 4 line if else into one line.
    – cropredy
    Jul 8 '19 at 11:43
1

Here:

myCase.Product_Total_Warranty_Days__c.intValue() + 5;

you are trying to get intValue() of null, which causes the error, just correct the code a little bit like:

if (myCase.Product_Total_Warranty_Days__c == null) {
    warrantyDays = 5;
} else {
    warrantyDays = myCase.Product_Total_Warranty_Days__c.intValue();
}
1
  • Thank you very much, Mariia. It worked (y)
    – Batir
    Jul 4 '19 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.