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
smohyee@ihrdc.comsmohyee@ihrdc.com 

Counting Active Contracts under Account. NullPointerException when testing.

I'm writing a simple trigger/class that, upon creation, editing or deletion of a Contract, counts all the 'Active' contracts underneath the related Account and updates the appropriate field on the Account record. 

 

I was up all last night trying to figure out the test failure errors I was getting, to no avail. I have a feeling somone on here will recognize the problem within 30 seconds =). By the way, I'm a newbie trying to learn, so any comments as to style, form and functionality are appreciated!

 

The error message is: 

 

Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Contract_Triggers: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.Contract_Handler.Count_Active_Contracts: line 14, column 1
Trigger.Contract_Triggers: line 11, column 1: []
Stack Trace	
Class.Contract_Test_Data.createAccountWithContracts: line 16, column 1 Class.Contract_Test_Methods.testContractCount_case1: line 9, column 1

 

I believe the problem lies somewhere between the actual Class and the Test Class responsible for creating test data. 

 

Here is the current form of the Contract_Handler class:

 

public class Contract_Handler{

    public static Integer Count_Active_Contracts(Account acc)

/*This method is passed an Account object, counts the related ACTIVE contracts(ie, in between start and end dates), and updates the Active_Contracts__c field on the account record. Count is also returned for other potential uses. - Sam Mohyee 7/18/2013*/ Integer count = 0; Date today = Date.today(); //ATTENTION: I Believe the issue has to do with this for loop, due to acc.Contracts not having any members (I tried a SOQL For Loop first and got the same error). for (Contract c : acc.Contracts){ if(today >= c.StartDate && today <= c.EndDate ){ count++; } } acc.Active_Contracts__c = count; update acc; return count; } }

 

Here is the test data I'm generating. ONLY TEST CASE == 1 is being tested right now, so I've omitted the code for the other cases:

 

@isTest
public class Contract_Test_Data{

    public static Account createAccountWithContracts(Integer testCase){
        /*3 test cases, based on types of contracts associated with account:
			1)Two Contracts, neither are active, one is expired, one is upcoming 
			2)Three Contracts, one active, one expired, one upcoming 
			3)Two Contracts, both active
		*/
        
        Account testAccount = createAccountRecord();
 
        
        if(testCase == 1){

            insert new Contract[]{new Contract(AccountId = testAccount.Id,
                                               Account = testAccount,
                			       StartDate = date.parse('01/01/2009'),
                                               ContractTerm = 12), 
                     	         new Contract(AccountId = testAccount.Id, 
                                               Account = testAccount,
                			       StartDate = date.parse('10/01/2014'),
                                               ContractTerm = 12)};
            
        }

        return testAccount;        	
    }
    
    
    private static Account createAccountRecord(){
        Account acc = new Account(Name = 'Test');
        
        insert acc;
        return acc;        
    }

    
}

 

 

My test trigger calls Contract_Test_Data.createAccountWithContracts(1), which causes the creation of the test Account record, along with two Contract records which will both qualify as INACTIVE. 

Avidev9Avidev9
Hopefully this one is a dupe .
Isn't your problem solved ?
smohyee@ihrdc.comsmohyee@ihrdc.com

Yeah this is a dupe, might have double clicked when I originally posted.

 

Good looking out though.