System.NullPointerException: Argument cannot be null

Im getting the error ‘System.NullPointerException: Argument cannot be null’ when I run my test class, in the below test method.

 /*
  *  update a existing report
  */
  static testMethod void test_updateExistingReport(){
    if (!setupComplete){
      setupTestData();
    }
    Report__c rsReport = [SELECT Id, Name, Date_Filter__c, Report_Settings__c, EndDate__c, StartDate__c FROM Report__c WHERE Id=:report.Id LIMIT 1];


    System.Test.setCurrentPage(new PageReference('/apex/x?sel-parent-grc__risk__c=grc__Business_Unit__c,grc__Category__c&sel-risk__risk_assessment__c=Risk__Risk_Assessment_Date__c,Risk__Inherent_Rating__c,Risk__Residual_Rating__c&selectedBusinessUnitId='+newBusinessUnit.Id));
    CreateReportController cls = new CreateReportController(new ApexPages.standardController(rsReport));

    for (ReportHelper.RelatedList rList : cls.relatedList){ //ERROR
      if (rList.objectName.toLowerCase() == 'risk__risk_assessment__c'){
        rList.isSelected = true;
      }
    }

    cls.reportFilters[0].selectedField = 'grc__category__c';
    cls.reportFilters[0].selectedOperator = 'equals';
    cls.reportFilters[0].obj.put('grc__category__c','Operational'); 
    cls.reportFilters[0].isCustomField = true;
    cls.reportFilters[0].isLookorPickOrDate = true;
    cls.reportFilters[0].isDependentOrMultiPick = false;
    cls.selectedDateFilterField = 'grc__last_review_date__c';
    cls.saveReport();

    Report__c newReport = [SELECT Id, Name, Date_Filter__c, Report_Settings__c, EndDate__c, StartDate__c FROM Report__c WHERE Id=:report.Id LIMIT 1];
    System.assertEquals('grc__last_review_date__c',newReport.Date_Filter__c);
    System.assertEquals(System.today().addDays(-15), newReport.StartDate__c);
    System.assert(newReport.Report_Settings__c != null);
  }

Error shows up in the for loop in the above test method (Marked as ‘//ERROR’ at the end of the line)

I wonder my method in the class should be changed. Below is my method in the class.

 public PageReference validateFilters(){
        // check the columns
        // Parent Object

        parentObj = ReportHelper.UpdateFields(parentObj, String.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('sel-parent-grc__risk__c')), parentObj.objName);

        // Related List
        for (ReportHelper.RelatedList rList : relatedList){
            if (rList.isSelected){
                rList.fieldsList = ReportHelper.UpdateFields(rList.fieldsList, String.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('sel-'+rList.objectName.toLowerCase())), rList.objectName);
            }
        }
        if (objName == parentObj.objName){ 
            updateFitler(reportFilters.get(filterNo-1));
        }else{
            for (ReportHelper.RelatedList rList : relatedList){
                if (objName == rList.objectName){
                    updateFitler(rList.relatedListFilters.get(filterNo-1));
                }
            }
        }
        return null;
    }

How can I fix this exception ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The problem will be in the constructor for CreateReportController. Is isn’t assigning a value to relatedList. So when you call

for (ReportHelper.RelatedList rList : cls.relatedList){ //ERROR
}

you are trying to iterate over null.

Check the constructor to ensure the relatedList is initialized.

Consider adding an assertion before the for loop to ensure the value is valid.

System.assertNotEquals(null, cls.relatedList, 'CreateReportController relatedList required');

If the Error line isn’t the For loop then it might be the rList.objectName being null and then calling .toLowerCase() on it.

Method 2

The problem will be in the constructor for CreateReportController. Is isn’t assigning a value to relatedList. So when you call

for (ReportHelper.RelatedList rList : cls.relatedList){ //ERROR
}
you are trying to iterate over null.

Check the constructor to ensure the relatedList is initialized.

Consider adding an assertion before the for loop to ensure the value is valid.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x