I'm attempting to write a test method for my constructor and when I get to a method in my non-test class where I'm assigning a value to the object passed I get a "attempt to de-reference null object error".
I know there are many topics on here but most that I've searched are "code specific". I've looked.
Here is my test method class for the constructor:
static testmethod void testConstructor(){
system.debug('****************************BEGIN TEST CONSTRUCTOR******************');
populateAll(); //writes test objects to the database???
test.startTest();
APM_Applications__c[] newRecords = database.query(getAllFieldsForSOQL());
system.debug(getAllFieldsForSOQL());
APM_ApplicationHandler ah = new APM_ApplicationHandler(newRecords);
ah.afterUpsert();
test.stopTest();
system.debug('****************************END TEST CONSTRUCTOR******************');
}
Here is my test class to populate data:
static testMethod map<string, id> populateAll(){
insert populateReferenceTable();
insert populateBlankApps();
map<string, id> apps = new map<string, id>();
list<APM_Applications__c> appIds = [select id from APM_Applications__c order by CreatedDate desc limit 2];
insert populateWarehouse(appIds[0].id, appIds[1].id);
apps.put('app1', appIds[0].id );
apps.put('app2', appIds[1].id );
list<Application_Component__c> components = [select id from Application_Component__c order by CreatedDate desc limit 6 ];
return apps;
}
static testmethod list<APM_Applications__c> populateBlankApps(){
list<APM_Applications__c> apps = new list<APM_Applications__c>();
apps.add(new APM_Applications__c(name='app1'));
apps[0].Annual_Cost_of_Ownership__c = 0;
apps.add(new APM_Applications__c(name='app2'));
apps[1].Annual_Cost_of_Ownership__c = 0;
return apps;
}
Here's where I'm getting the error:
public static void updateTotalCost(APM_Applications__c app){
system.debug(app.id); //APP ID EXISTS MEANING APP EXISTS
if (app.id != null){ //defensive coding just in case
app.Annual_Cost_of_Ownership__c = app.Labor_Cost__c + app.Licensing_Cost__c + app.Infrastructure_Cost__c; //error happens here
}
}