+ Start a Discussion
Collen Mayer 6Collen Mayer 6 

System.NullPointerException in Test Class

Hi All,
I'm getting an error when trying to write my test class for my controller extension: 

System.NullPointerException: Attempt to de-reference a null object//Class.Invoice.<init>: line 20, column 1
Class.TestClassforInvoice.testmyInvoices: line 25, column 1


Here's my test class:
@istest
public class TestClassforInvoice {
 public static testMethod void testmyInvoices(){
	Account testAccount = new Account (Name = 'MyHousehold');
    Insert testAccount; 
    Contact testContact = new Contact (LastName= 'smith');
    insert testContact;
    AECaseMgmt__Program__c programNew = new AECaseMgmt__Program__c(Name= 'test prg'); 
	insert programNew; 
    AECaseMgmt__Program_Case__c testCase = new 	AECaseMgmt__Program_Case__c 
        (AECaseMgmt__Open_Date__c = System.now().date(), 	
         Program__c = programNew.Id, BypassValidationForProcessBuilder__c = true); 
     testCase.AECaseMgmt__Household__c = testAccount.id; 
    insert testCase;
    Case_Member_Enrollments__c testmember = new 
        Case_Member_Enrollments__c (Name = 'testCM', Case_ID__c = testCase.id, BypassValidationForProcessBuilder__c=true );
    
    testmember.Case_Member__c = testContact.id;
     insert testmember; 
    
     Client_Billing__c clientbill = new Client_Billing__c 
         (Entry_Type__c = 'Planned Expense', Billing_Item__c = 'Court Report', Client_Case__c = testCase.Id);
     
     ApexPages.StandardController sc = new ApexPages.StandardController(testcase);
		Invoice  controller = new Invoice(sc);

		PageReference pageRef = Page.SingleInvoice;
        Test.setCurrentPage(pageRef);

 }
}
The problem seems to be here: 

ApexPages.StandardController sc = new ApexPages.StandardController(testcase); Invoice controller = new Invoice(sc);

This is my extension:
 
public class Invoice {
    public ApexPages.StandardController stdCntrlr {get; set;}
    public list <CasewInvoices> caselist {get; set;}
    public list <Client_Billing__c> tempinvoicelist {get; set;}
    public String currentRecordId {get;set;}
    public decimal thirtydayold {get; set;}
    public decimal sixtydayold {get;set;}
    public decimal ninetydayold {get;set;}
    
    public Invoice(ApexPages.StandardController controller) {
        stdCntrlr = controller;
        if (caselist==null){
            caselist = new List <CasewInvoices>();
            }
        
        tempinvoicelist = [Select ID, Client_Case__c, Name, Amount__c, Date__c,Transaction_Description__c, Amount_Credit_Debit__c 
                           from Client_Billing__c Where Include_on_Invoice__c = true ]; 
		currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');      
     	
        String pageName = ApexPages.currentPage().getUrl().substringAfter('apex/');
		system.debug('-----current page name----'+pagename);

// populate case list; if from single invoice, just get one case; otherwise get all        
        
        If (pageName.contains('SingleInvoice'))
        {
        	for (AECaseMgmt__Program_Case__c  record: [Select ID, AECaseMgmt__Household__r.BillingSTreet, AECaseMgmt__Household__r.BillingCity,
                                                   AECaseMgmt__Household__r.BillingState,AECaseMgmt__Household__r.BillingPostalCode,
                                                   AECaseMgmt__Household__r.npe01__One2OneContact__r.name,
                                                   Program__r.name, Name,Account_Balance__c from  AECaseMgmt__Program_Case__c 
                                                   where Id =: currentRecordId])  
        	{
           		 caselist.add(New CasewInvoices (record));
        	}
        }
		Else
        {
            for (AECaseMgmt__Program_Case__c  record: [Select ID, AECaseMgmt__Household__r.BillingSTreet, AECaseMgmt__Household__r.BillingCity,
                                                   AECaseMgmt__Household__r.BillingState,AECaseMgmt__Household__r.BillingPostalCode,
                                                   AECaseMgmt__Household__r.npe01__One2OneContact__r.name,
                                                   Program__r.name, Name,Account_Balance__c from  AECaseMgmt__Program_Case__c  where Id IN (SELECT Client_Case__c FROM Client_Billing__c)])  
        	{
            	caselist.add(New CasewInvoices (record));	
        	}
        }
        
        for (CasewInvoices caseincrement:caselist)
        {
            Decimal thirtydayold = 3;
            for(Integer i = 0; i <tempinvoicelist.size(); i++){
            	if (tempinvoicelist[i].client_case__c ==caseincrement.clientcase.id)
                    {
                        caseincrement.billinglist.add (tempinvoicelist[i]);
                        If (tempinvoicelist[i].Amount_Credit_Debit__c>0)
                        {
                        	If (tempinvoicelist[i].date__c.daysBetween(date.today())> 90)
                        		caseincrement.ninetydayold = caseincrement.thirtydayold + tempinvoicelist[i].Amount_Credit_Debit__c;
                        
                            If (tempinvoicelist[i].date__c.daysBetween(date.today())> 60)
                        		caseincrement.sixtydayold = caseincrement.thirtydayold + tempinvoicelist[i].Amount_Credit_Debit__c;
                        
                            If (tempinvoicelist[i].date__c.daysBetween(date.today())> 30)
                        		caseincrement.thirtydayold = caseincrement.thirtydayold + tempinvoicelist[i].Amount_Credit_Debit__c;
                        }
                        Else 
                        {
                            caseincrement.ninetydayold = caseincrement.ninetydayold + tempinvoicelist[i].Amount_Credit_Debit__c;
                            caseincrement.sixtydayold = caseincrement.sixtydayold + tempinvoicelist[i].Amount_Credit_Debit__c;
                            caseincrement.thirtydayold = caseincrement.thirtydayold + tempinvoicelist[i].Amount_Credit_Debit__c;
                        }
                    }
            }
        }

 }

	Public class CasewInvoices
    {
        public List<Client_Billing__c> billingList {get;set;}
        public AECaseMgmt__Program_Case__c Clientcase {get;set;}
        public decimal thirtydayold {get;set;}
        public decimal sixtydayold {get;set;}
        public decimal ninetydayold {get;set;}
        
        public CasewInvoices (AECaseMgmt__Program_Case__c cc)
        {
            Clientcase = cc;
            billingList = new list<Client_Billing__c>();
            thirtydayold = 0;
            sixtydayold = 0;
            ninetydayold = 0;
        }
	}
}
Can someone help me troubleshoot this error, please?  Many thanks.

Collen

 
Best Answer chosen by Collen Mayer 6
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
        PageReference pageRef = Page.SingleInvoice;
        Test.setCurrentPage(pageRef);
        ApexPages.StandardController sc = new ApexPages.StandardController(testcase);
        Invoice  controller = new Invoice(sc);



Let us know if this will help you