I have a custom controller for a VF page that allows users to add and remove rows of records to create before they are actually created. My test class covers the addition of rows, but it does not cover the removal. I get the error:
System.NullPointerException: Argument cannot be null.
Class.NewRecController.RemoveType: line 32, column 1
Class.Test_NewRecController.testMethod1: line 29, column 1
The lines that are not being covered are 32-35 & 44-46 in my controller below. Can anyone help on how I can get these covered?
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;
PageReference pageRef = Page.NewRecType;
pageRef.getParameters().put('contId','testContact.Id');
pageRef.getParameters().put('acctId','testAccount.Id');
pageRef.getParameters().put('Rec_Type__c','Type-1');
Test.StartTest();
NewRecController testNewRec = new NewRecController();
testNewRec.saveType();
NewRecController testNewRec2 = new NewRecController();
testNewRec2.addRecType();
NewRecController testNewRec3 = new NewRecController();
testNewRec3.rec = 1;
testNewRec3.removeType();
Test.StopTest();
}
}
Controller
public class NewRecController {
Rec_Type__c rec1 = new Rec_Type__c();
public list<Rec_Type__c> listRecType{ get; set; }
Id acctId;
Id contId;
Public Integer rec {get; set;}
Public List<Rec_Type__c> ContRecList {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(rec1);
}
Public void addRecType() {
Rec_Type__c rec1 = new Rec_Type__c();
rec1.Contact__c = contId;
rec1.Account__c = acctId;
listRecType.add(rec1);
}
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;
}
}