function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bhupathi YadavBhupathi Yadav 

I am getting System.NullPointerException: Attempt to de-reference a null object when i try to run test class

This is mycode

Public class OrderApprvlUtility
{
   
     @InvocableMethod
    public static void submitForApproval(List<ccrz__E_Order__c> ordids)
    {
       set<id> orderidsset=new set<id>();
        for(ccrz__E_Order__c Ordr:ordids)
        {
            orderidsset.add(Ordr.id);  
        }
     system.debug(' Inside approval--->' +ordids);
      List<ccrz__E_Order__c> orderList=[select id,createdDate,ccrz__OrderStatus__c,ccrz__Account__r.ccrz__E_AccountGroup__c,(select ccrz__Product__r.RLWS_CF_RL_Merch_Division__c from ccrz__E_OrderItems__r ORDER BY CREATEDDATE ASC LIMIT 1) from ccrz__E_Order__c where id =:orderidsset];
     system.debug(' Inside approval-AFTER -->' +orderList);
        Map<Id,String> orderItemsMerchDiv = new Map<Id,String>();
        Set<Id> accountGrpIds= new Set<Id>();
          //[0].ccrz__E_OrderItems__r
       List<String> merchString=new List<String>();
         Map<Id,ccrz__E_Order__c> orderDateMap = new  Map<Id,ccrz__E_Order__c>();
      for(ccrz__E_Order__c cc: orderList)
      {
          system.debug(' cc.ccrz__E_OrderItems__r' +cc.ccrz__E_OrderItems__r +' status '+cc.ccrz__OrderStatus__c);
          if(null!= cc.ccrz__E_OrderItems__r && cc.ccrz__E_OrderItems__r.size()>0){
           orderItemsMerchDiv.put(cc.id, cc.ccrz__E_OrderItems__r[0].ccrz__Product__r.RLWS_CF_RL_Merch_Division__c);
           orderDateMap.put(cc.id,cc);
           accountGrpIds.add(cc.ccrz__Account__r.ccrz__E_AccountGroup__c);
         }           
      }
       // system.debug('#######:' +merchString);
        List<ccrz__E_AccountGroupPriceList__c> Acgplst = [select ccrz__AccountGroup__c,RLWS_CF_Account_Executive__r.id, RLWS_CF_Account_Executive__r.name,RLWS_CF_Merch_Div__c from ccrz__E_AccountGroupPriceList__c where RLWS_CF_Merch_Div__c =:orderItemsMerchDiv.values() and ccrz__AccountGroup__c=:accountGrpIds ];
       // system.debug([select RLWS_CF_Account_Executive__r.name from ccrz__E_AccountGroupPriceList__c where RLWS_CF_Merch_Div__c ='merchString']);
        Map<String,Id> merchDIVAEMaP=  new Map<String,Id>();
       
        System.debug('Merch div:' +Acgplst );
        for(ccrz__E_AccountGroupPriceList__c accGrp : Acgplst){
            merchDIVAEMaP.put(accGrp.RLWS_CF_Merch_Div__c+'!'+accGrp.ccrz__AccountGroup__c, accGrp.RLWS_CF_Account_Executive__r.id);
        }
        System.debug('Account Executive:' +merchDIVAEMaP);
        Map<id,String> orderids=new Map<id,String>();
         Approval.ProcessSubmitRequest req ;
         Approval.ProcessResult result;
        List<Task> taskList = new List<Task>();
        Task taskObj;
        ccrz__E_Order__c ordObj;
        String orderKey;
        for(Id OrderId :orderDateMap.keySet())
        {
            //orderids.put(Ordr.id,Ordr.Name);
             req= new Approval.ProcessSubmitRequest();
            req.setComments('Submiting for Approval');
             req.setObjectId(OrderId);
            ordObj = orderDateMap.get(OrderId);
            orderKey = orderItemsMerchDiv.get(OrderId)+'!'+ordObj.ccrz__Account__r.ccrz__E_AccountGroup__c;
            req.setNextApproverIds(new Id[] {merchDIVAEMaP.get(orderKey)});
            result = Approval.process(req);
        //    ordObj  = new ccrz__E_Order__c(Id=orderId);
          //     orderDateMap.get(orderId).RLWS_CF_Order_Approver__c = merchDIVAEMaP.get(orderItemsMerchDiv.get(orderId));
           taskObj = createTaskForApproval(merchDIVAEMaP.get(orderKey), ordObj.id,ordObj.createdDate);
            taskList.add(taskObj);
        }
        
        if(!taskList.isEmpty()){
            Database.DMLOptions dmlo = new Database.DMLOptions();
            dmlo.EmailHeader.triggerUserEmail = true;
            Database.insert(taskList, dmlo);
        }
    }  
    
    private static Task createTaskForApproval(Id assignId,Id orderId,DateTime orderCreatedDate){
             Integer taskDays = Integer.valueOf(RLWS_Obj_Task_Duration__c.getInstance('Task Duration').RLWS_CF_Task_Due_Date_duration__c);
             Integer reminderHrs = Integer.valueOf(RLWS_Obj_Task_Duration__c.getInstance('Task Duration').RLWS_CF_Task_Reminder_Hours__c);
             Task taskObj = new Task();
             taskObj.OwnerId = assignId;
             taskObj.WhatId = orderId;
             taskObj.IsReminderSet = true;
             taskObj.ReminderDateTime = orderCreatedDate.addHours(reminderHrs); 
             taskObj.Subject = 'Task to Approve Order';
             taskObj.Priority = 'Medium';
             taskObj.Status = 'Not Started';
             taskObj.Type = 'Email'; 
             taskObj.RLWS_CF_From_Order__c = True;
                taskObj.ActivityDate=Date.valueOf(orderCreatedDate.addDays(taskDays));
             taskObj.Description = 'Task for Approve Order';
            // taskObj.RLWS_CF_Task_Type__c ='From Order';
             return taskObj;
        }

}


Note: New to Apex coding 
 Can you please help to cover this in test class
my test class

@isTest
public class RLWS_Cls_OrderApprvlUtility_Test {
 static testMethod void myTest() {
   List<account> accts;
   Account a = new Account(Name='TestAccount');
            accts.add(a);
  //Create an Task  Record
     Task taskObj = new Task();
             taskObj.OwnerId = UserInfo.getUserId();
             taskObj.IsReminderSet = true;
             taskObj.Subject = 'Task to Approve Order';
             taskObj.Priority = 'Medium';
             taskObj.Status = 'Not Started';
             taskObj.Type = 'Email'; 
             taskObj.ActivityDate=Date.today().addDays(7);
             taskObj.Description = 'Task for Approve Order';
        insert taskObj ;
            
    
   List<ccrz__E_Order__c> ordersToCreate=  new List<ccrz__E_Order__c>();
        ccrz__E_Order__c  orderRecord = new ccrz__E_Order__c   ();
            
          /*  orderRecord.ccrz__BuyerLastName__c = 'Test1';  
            orderRecord.ccrz__OrderStatus__c= 'Order Submitted';
            ordersToCreate.add(orderRecord);
            insert ordersToCreate; 
            */
            for(Integer i=6;i<=100;i++)
              {
               orderRecord=new ccrz__E_Order__c(ccrz__BuyerLastName__c='Test1',ccrz__OrderStatus__c='Order Submitted');
               ordersToCreate.add(orderRecord);
              }
              insert ordersToCreate;
            
      test.startTest();
      
  //RLWS_Cls_OrderApprvlUtility Ordersubmit = new RLWS_Cls_OrderApprvlUtility ();
  RLWS_Cls_OrderApprvlUtility.submitForApproval(ordersToCreate);
  test.stopTest();
  }
}
RD@SFRD@SF
Hi Bhupati,

Just a quick shot in the dark.

In the test class, you have initialized the account but have not inserted or linked it to the order. And I think the account is being referred in the main class you are trying to write the test class for is using the account and hence is giving the null reference error.

Hope it helps
RD