You need to sign in to do that
Don't have an account?
mohan s 37
First error:attemp to de-reference a null object
Hi Friends,
How to resolve System.NullPointerException Attempt to de-reference a null object in batch. Following is my code. Please suggest where do I made mistake in the below code. Out of 40 batches only one batch got failed.
global class UpdateBatch implements Database.Batchable<Sobject>,Schedulable{
global string query = '';
public static boolean issiteLocAuthrzsBatch =false;
public Map<String,Map<String,Map<String,Prod_Authorization__c>>> hqmap = new Map<String,Map<String,Map<String,Prod_Authorization__c>>>();
public List<Prod_Authorization__c> listToUpdate = new List<Prod_Authorization__c>();
global id partnerRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner').getRecordTypeId();
global void execute(schedulableContext sc){
}
global database.QueryLocator start(database.BatchableContext bc){
query = 'select id,Account__c,Authorization_Type__c,Product_Name__c,Original_Active_Date__c,Status__c,Product_Category__c,Prod_Auth_Id__c, ' +
' Account__r.Location_Type__c,Account__r.ParentId,Auth_Track_Sales__c,Sales_n__c,Auth_Track_Design__c,Design_n__c, ' +
' Auth_Track_Implement__c,Implement_n__c,Auth_Track_Maintain__c,Maintain_n__c,Account__r.Status__c FROM Prod_Authorization__c '+
' WHERE Account__r.RecordTypeId=\''+partnerRecTypeId+'\' and Account__r.Status__c =\'Active\'';
return database.getQueryLocator(query);
}
global void execute(database.BatchableContext bc, List<Prod_Authorization__c>authorizations){
for(Prod_Authorization__c pa : authorizations){
if(pa.Account__r.Location_Type__c == 'Headquarters' && pa.Account__r.Status__c=='Active'){
if(!hqmap.containsKey(pa.Account__c)){
hqmap.put(pa.Account__c,new Map<String,Map<String,Prod_Authorization__c>>{pa.Product_Name__c=>new Map<String,Prod_Authorization__c>{pa.Authorization_Type__c=>pa}});
}else{
if(hqmap.get(pa.Account__c).containsKey(pa.Product_Name__c)){
hqmap.get(pa.Account__c).get(pa.Product_Name__c).put(pa.Authorization_Type__c,pa);
}else {
hqmap.get(pa.Account__c).put(pa.Product_Name__c,new Map<String,Prod_Authorization__c>());
hqmap.get(pa.Account__c).get(pa.Product_Name__c).put(pa.Authorization_Type__c,pa);
}
}
}
}
for(Prod_Authorization__c pa : authorizations){
if(pa.Account__r.Location_Type__c == 'Site Location' && pa.Account__r.Status__c== 'Active' &&
pa.Account__r.ParentId!=null && hqmap.containsKey(pa.Account__r.ParentId)){ // here I got exception
pa.Status__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Status__c;
pa.Original_Active_Date__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Original_Active_Date__c;
pa.Product_Category__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Product_Category__c;
pa.Auth_Track_Sales__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Sales__c;
pa.Sales_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Sales_n__c;
pa.Auth_Track_Design__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Design__c;
pa.Design_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Design_n__c;
pa.Auth_Track_Implement__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Implement__c;
pa.Implement_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Implement_n__c;
pa.Auth_Track_Maintain__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Maintain__c;
pa.Maintain_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Maintain_n__c;
listToUpdate.add(pa);
}
}
issiteLocAuthrzsBatch = true;
if(!listToUpdate.isEmpty()){
try{
Database.SaveResult[] result =database.update(listToUpdate,false);
}Catch(Exception e){
system.debug('Exception:: '+e.getMessage());
}
}
}
global void finish(database.BatchableContext bc){
}
}
Thanks,
Mohan S
How to resolve System.NullPointerException Attempt to de-reference a null object in batch. Following is my code. Please suggest where do I made mistake in the below code. Out of 40 batches only one batch got failed.
global class UpdateBatch implements Database.Batchable<Sobject>,Schedulable{
global string query = '';
public static boolean issiteLocAuthrzsBatch =false;
public Map<String,Map<String,Map<String,Prod_Authorization__c>>> hqmap = new Map<String,Map<String,Map<String,Prod_Authorization__c>>>();
public List<Prod_Authorization__c> listToUpdate = new List<Prod_Authorization__c>();
global id partnerRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner').getRecordTypeId();
global void execute(schedulableContext sc){
}
global database.QueryLocator start(database.BatchableContext bc){
query = 'select id,Account__c,Authorization_Type__c,Product_Name__c,Original_Active_Date__c,Status__c,Product_Category__c,Prod_Auth_Id__c, ' +
' Account__r.Location_Type__c,Account__r.ParentId,Auth_Track_Sales__c,Sales_n__c,Auth_Track_Design__c,Design_n__c, ' +
' Auth_Track_Implement__c,Implement_n__c,Auth_Track_Maintain__c,Maintain_n__c,Account__r.Status__c FROM Prod_Authorization__c '+
' WHERE Account__r.RecordTypeId=\''+partnerRecTypeId+'\' and Account__r.Status__c =\'Active\'';
return database.getQueryLocator(query);
}
global void execute(database.BatchableContext bc, List<Prod_Authorization__c>authorizations){
for(Prod_Authorization__c pa : authorizations){
if(pa.Account__r.Location_Type__c == 'Headquarters' && pa.Account__r.Status__c=='Active'){
if(!hqmap.containsKey(pa.Account__c)){
hqmap.put(pa.Account__c,new Map<String,Map<String,Prod_Authorization__c>>{pa.Product_Name__c=>new Map<String,Prod_Authorization__c>{pa.Authorization_Type__c=>pa}});
}else{
if(hqmap.get(pa.Account__c).containsKey(pa.Product_Name__c)){
hqmap.get(pa.Account__c).get(pa.Product_Name__c).put(pa.Authorization_Type__c,pa);
}else {
hqmap.get(pa.Account__c).put(pa.Product_Name__c,new Map<String,Prod_Authorization__c>());
hqmap.get(pa.Account__c).get(pa.Product_Name__c).put(pa.Authorization_Type__c,pa);
}
}
}
}
for(Prod_Authorization__c pa : authorizations){
if(pa.Account__r.Location_Type__c == 'Site Location' && pa.Account__r.Status__c== 'Active' &&
pa.Account__r.ParentId!=null && hqmap.containsKey(pa.Account__r.ParentId)){ // here I got exception
pa.Status__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Status__c;
pa.Original_Active_Date__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Original_Active_Date__c;
pa.Product_Category__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Product_Category__c;
pa.Auth_Track_Sales__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Sales__c;
pa.Sales_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Sales_n__c;
pa.Auth_Track_Design__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Design__c;
pa.Design_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Design_n__c;
pa.Auth_Track_Implement__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Implement__c;
pa.Implement_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Implement_n__c;
pa.Auth_Track_Maintain__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Auth_Track_Maintain__c;
pa.Maintain_n__c = hqmap.get(pa.Account__r.ParentId).get(pa.Product_Name__c).get(pa.Authorization_Type__c).Maintain_n__c;
listToUpdate.add(pa);
}
}
issiteLocAuthrzsBatch = true;
if(!listToUpdate.isEmpty()){
try{
Database.SaveResult[] result =database.update(listToUpdate,false);
}Catch(Exception e){
system.debug('Exception:: '+e.getMessage());
}
}
}
global void finish(database.BatchableContext bc){
}
}
Thanks,
Mohan S
Raj Vakati
Try like this
mohan s 37
Hi RajaMohan,
Thanks for your quick reply. I have tried what you have suggested but that is also not working.
Thanks,
Mohan S