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
John Neilan 18John Neilan 18 

Test Class to Cover VF Dynamic Removal of Rows

Hi,

I have a VF page that allows a user to dynamically add and remove rows before saving each row as a new custom object record.  I am trying to put together a test class to cover the removal part of my controller, but I can't seem to get any values through to the test as I am getting an error:
 
System.NullPointerException: Argument cannot be null.

Class.NewRecController.RemoveType: line 24, column 1
Class.Test_NewRecController.testMethod1: line 27, column 1

What am I missing?

Controller
public class NewRecController {

    Rec_Type__c rec = new Rec_Type__c();
    public list<Rec_Type__c> listRecType{ get; set; }

    Id acctId;
    Id contId;
    Public Integer rec {get; set;}
    
//    Constructor 

	public NewRecController() { 
        acctId = ApexPages.currentPage().getParameters().get('acctId');
        contId = ApexPages.currentPage().getParameters().get('contId');
        
        rec.Account__c = acctId;
        rec.Contact__c = contId;
        
        listRecType = new list<Rec_Type__c>();
        listRecType.add(rec);
	} 


	public PageReference RemoveType() {
    	rec = Integer.valueOf(ApexPages.currentPage().getParameters().get('rec'));
        listRecType.remove(rec);
        return null;
    }
     
    public PageReference saveType() {
    	try {
        	insert listRecType;
    	} catch(DmlException e) {
        	ApexPages.addMessages(e);
        return null;
    	}
        PageReference contRecord = new PageReference('/'+contId);
        contRecord.setRedirect(true);
    return contRecord;
    }
}

Test Class
@isTest 
private class Test_NewRecController {

    static testMethod void testMethod1() 
    {
        Account testAccount = test_CreateRecords.createAcct(0);
        insert testAccount;
        
        Contact testContact = test_CreateRecords.createCont(testAccount.Id);
        insert testContact;
        
	Test.StartTest();
        PageReference pageRef = Page.NewRecType;
        	pageRef.getParameters().put('contId','testContact.Id');
        	pageRef.getParameters().put('acctId','testAccount.Id');
        	pageRef.getParameters().put('Rec_Type__c','Type 2');
        
        NewRecController testNewRec = new NewRecController();
        	testNewRec.rec = 1;
        
        testNewRec.RemoveType();
        testNewRec.saveType();

	Test.StopTest();
    }
}

​​​​​​​
AnudeepAnudeep (Salesforce Developers) 
Hi John - Can you add a debug after this line in your apex class to confirm if it is returning a value?
 
rec = Integer.valueOf(ApexPages.currentPage().getParameters().get('rec'));

I recommend adding a try and catch block as described here and doing null checks wherever necessary
 
John Neilan 18John Neilan 18
Thanks Anudeep. I did add a debug statement and it returns a value of 0, not Null, but my test class does not find any records.  The method is designed to remove rows from the VF page before saving. Any ideas?