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){
        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].isCustomField = true;
        cls.reportFilters[0].isLookorPickOrDate = true;
        cls.reportFilters[0].isDependentOrMultiPick = false;
        cls.selectedDateFilterField = 'grc__last_review_date__c';
        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(, 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){ 
                for (ReportHelper.RelatedList rList : relatedList){
                    if (objName == rList.objectName){
            return null;

    How can I fix this exception ?

  • 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.

    I agree with your suggestion.Related list is already initialized. I tried the assertion and it doesn't fail. I tried adding a null check, to both scenarios (Related list and the object name) but still it fails.

    @Butterfly Can you check exactly which line is throwing the NullPointerException? What do the RelatedList `objectName` and `isSelected` properties do? I.e. do they have a implementation that could cause a null reference.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM