+ Start a Discussion
Vineet Anand 5Vineet Anand 5 

Apex Null Pointer Error

Hi All,
I am facing Below error for my piece of code. Any suggestion / help is highly Appreciable.

Error:
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Update failed. First exception on row 0 with id 5007F00000tAkWFQA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Class.MaintenanceRequestHelper.cycleCalc2: line 23, column 1 Class.MaintenanceRequestHelper.updateWorkOrders: line 14, column 1 Trigger.MaintenanceRequest: line 7, column 1: []

Trigger:
trigger MaintenanceRequest on Case (after update, after insert) {
    Public List<Case> closedCase = new List<Case>();
    List<Case> caseA = [select ID, vehicle__c, equipment__c, date_due__c from Case Where Id IN :Trigger.new AND status ='Closed' and Type IN ('Repair', 'Routine Maintenance')];
    for(Case a : caseA) {
        closedCase.add(a);
    }
     MaintenanceRequestHelper.updateWorkOrders(closedCase); 
}

MaintenanceRequestHelper:

public class MaintenanceRequestHelper {
    Public Static List<Case> oldCase = new List<Case>();
    public static void updateWorkOrders(List<Case> closedCase){
        oldCase = closedCase;
        List<Case> newCase = new List<Case>();
            for (Case newCase2 : oldCase) {
                 newCase.add(new Case(type = 'Routine Maintenance',
                                status = 'New',
                                origin = 'Web',
                                Subject = null,
                                Date_Reported__c = date.today(),
                                Vehicle__c = newCase2.Vehicle__c,
                                Equipment__c = newCase2.Equipment__c,
                                Date_Due__c = cycleCalc2()));  
        }
        insert newCase;
    }

    private static Date cycleCalc2() {        
        oldCase = [select ID from Case Where status = 'Closed' and Type IN ('Repair', 'Routine Maintenance')];
        AggregateResult[] minCycleDay2 = [Select min(Equipment__r.Maintenance_Cycle__c) FROM Work_Part__c  WHERE Equipment__r.Maintenance_Cycle__c != null AND         Maintenance_Request__c IN: oldCase];
        Integer minCycleNum = ((Decimal)minCycleDay2[0].get('expr0')).intValue();
        Date returnDate = date.Today() + minCycleNum;
        return returnDate;
        }
    }

Please help.
Rgd's
Jessica RiffeJessica Riffe
I belive your minCycleNum value is null, which is why you are getting the null pointer exception.  Add some debug statements and check the logs to check your logic and make sure your minCycleNum is getting populated as expected.

I also think you will want to do you date as system.today().addDays(minCycleNum); as your issue could also be that date is null in date.Today()
Deepali KulshresthaDeepali Kulshrestha
Hi Vineet,

You are putting null in your subject field.
I suggest you put '' if you want to leave the subject field blank.

like this:
Subject = '';

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com