You need to sign in to do that
Don't have an account?
foodrunner
System.NullPointerException: Attempt to de-reference a null object
In the test class that I am writing I am recieving the following error:
System.NullPointerException: Attempt to de-reference a null object Class.testForecastOverride.TestOverrideTrigger: line 12, column 9 External entry point
Below is my test class:
@isTest private class testForecastOverride { testmethod private static void TestOverrideTrigger() { Opportunity o; id id1 = userinfo.getProfileId(); Map <Id, Opportunity> opp = new map <id, Opportunity>(); If (id1 == '00e30000000gAk7') { o.ForecastCategoryName = o.Forecast_Category_Override__c; } else if (id1 == '00eQ0000000HivN') { o.ForecastCategoryName = o.Forecast_Category_Override__c; } else if (id1 == '00e30000000gl0u') { o.ForecastCategoryName = o.Forecast_Category_Override__c; } } }
Below is my trigger
trigger ForecastOverride on Opportunity (Before Update) { Set<Id> bIds = new Set<Id>(); for(Opportunity op : trigger.new){ System.debug('**** 0 op id : '+op.ForecastCategoryName); bids.add(op.id); } list<Opportunity> forecastcategoryList = new List<Opportunity>(); if(StaticClass.doNotExecute ==true) { system.debug('Inserting'+StaticClass.doNotExecute); for(Opportunity o:trigger.new) { bIds.add(o.id); id id1 = userinfo.getProfileId(); If (id1 == '00e30000000gAk7') { if (trigger.new[0].Forecast_Category_Override__c != trigger.old[0].Forecast_Category_Override__c) { o.ForecastCategoryName = o.Forecast_Category_Override__c; } } else if (id1 == '00eQ0000000HivN') { if (trigger.new[0].Forecast_Category_Override__c != trigger.old[0].Forecast_Category_Override__c) { o.ForecastCategoryName = o.Forecast_Category_Override__c; } } else if (id1 == '00e30000000gl0u') { if (trigger.new[0].Forecast_Category_Override__c != trigger.old[0].Forecast_Category_Override__c) { o.ForecastCategoryName = o.Forecast_Category_Override__c; } } } } }
I believe I am needing to write a map/list of the opportunity in the test class, but how do I do this?
Thank you again for your help
@isTest
private class testForecastOverride {
testmethod private static void TestOverrideTrigger() {
try
{
Opportunity o;
id id1 = userinfo.getProfileId();
Map <Id, Opportunity> opp = new map <id, Opportunity>();
If (id1 == '00e30000000gAk7') {
o.ForecastCategoryName = o.Forecast_Category_Override__c;
}
else if (id1 == '00eQ0000000HivN') {
o.ForecastCategoryName = o.Forecast_Category_Override__c;
} else if (id1 == '00e30000000gl0u') {
o.ForecastCategoryName = o.Forecast_Category_Override__c;
}
}
}
catch(Exception e)
{
system.debug('Error'+ e);
}
}
Hope this helps.
All Answers
Please do encapsulate your code in a proper tyy - catch block and also insert checks for null before you try to access a field of the object, I think doing the above should correct the problem faced by you.
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Thank you for your post. I have not used a try-catch block before. Can you point me to some examples i can study?
@isTest
private class testForecastOverride {
testmethod private static void TestOverrideTrigger() {
try
{
Opportunity o;
id id1 = userinfo.getProfileId();
Map <Id, Opportunity> opp = new map <id, Opportunity>();
If (id1 == '00e30000000gAk7') {
o.ForecastCategoryName = o.Forecast_Category_Override__c;
}
else if (id1 == '00eQ0000000HivN') {
o.ForecastCategoryName = o.Forecast_Category_Override__c;
} else if (id1 == '00e30000000gl0u') {
o.ForecastCategoryName = o.Forecast_Category_Override__c;
}
}
}
catch(Exception e)
{
system.debug('Error'+ e);
}
}
Hope this helps.
Thank you. I was trying to include three sets of try for each if statement thinking the try statement would replace each if statements. But it appears like the try statement is to encompass if statements. Thank you again for your help.