You need to sign in to do that
Don't have an account?
Attempt to de-reference a null object issue - though non-null value is there
Hi,
I am trying to run the test class for the below apex class, but I am getting issue attempt to de-reference a null object:
at line "ownerids.add(AssetRecord.OwnerId)".
Even though I am able to see the value at line system.debug('**** AssetRecords OwnerId ' + AssetRecord.OwnerId);
APEX CLASS:
------------------
global class OpportunitiesAmounts implements
Database.Batchable<SObject>{
private final List<Id> ownerids;
private final String template = 'Sample Template';
private static Map<Id,Id> opportunityrevenueMap = new Map<Id,Id>();
private static Map<Id,string> EquipmentrevenueMap = new Map<Id,string>();
global Database.queryLocator
start(Database.BatchableContext ctx){
string query = 'select Id, Equipment_Title__c, Total_Revenue__c from Equipment__c where Total_Revenue__c >= 100000' ;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
for(Sobject EquipmentItem : scope )
{
EquipmentrevenueMap.put(string.valueof(EquipmentItem.get('Id')), string.valueof(EquipmentItem.get('Equipment_title__c')));
}
List<Opportunity> opportunityEquipmentList = [select Id, Equipment__c, Asset__c from Opportunity
where Equipment__c IN :EquipmentrevenueMap.KeySet()];
for(Opportunity opportunityItem : opportunityEquipmentList)
{
opportunityrevenueMap.put(opportunityItem.Asset__c,opportunityItem.Id);
}
for(Asset__c AssetRecord : [select Id, OwnerId,Name from Asset__c
where Id in :opportunityrevenueMap.KeySet()]) {
system.debug('**** AssetRecords Id ' + AssetRecord.Id);
system.debug('**** AssetRecords Name ' + AssetRecord.Name);
system.debug('**** AssetRecords OwnerId ' + AssetRecord.OwnerId);
ownerids.add(AssetRecord.OwnerId);
}
if(ownerids.size() > 0) {
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(ownerids);
mail.setTemplateId([select Id from EmailTemplate where Name = :template].Id);
Messaging.SendEmail(new Messaging.MassEmailMessage[] { mail });
}
}
global void finish(Database.BatchableContext BC)
{
}
}
Note: Infact I am populating owner id value with UserInfo.getUserId() in test class and the system.debug('**** AssetRecords OwnerId ' + AssetRecord.OwnerId) also displays the same id.
Please help me on resolving this issue.
Thanks,
JBabu.
Try changing
private final List<Id> ownerids;
private final List<Id> ownerids = new list<id>();
All Answers
Try changing
private final List<Id> ownerids;
private final List<Id> ownerids = new list<id>();
Thanks a lot Abhay. I somehow missed it.
Thanks,
JBabu.