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
Jesus FerrerasJesus Ferreras 

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

Hello, 

 

How I can do this assignment correctly?

 

Thanks

 

 

global Database.Querylocator start(Database.BatchableContext bc){
//where fecha_del_evento__c>=LAST_N_DAYS:1
return Database.getQueryLocator([SELECT ID, Comentarios_Invitados__c,EventRelationId__c,Estado_Invitado__c,fecha_del_evento__c FROM Informe_icex__c]);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){

Map<ID, Informe_icex__c> iMap = new Map<ID, Informe_icex__c>((List<Informe_icex__c>)scope);
System.Debug('iMap Records : ' + iMap);

Set<Id> sIdInforme = new Set<Id>();
Informe_icex__c ii;

for(sObject c:scope) {
ii = (Informe_icex__c) c;
if(!sIdInforme.contains(ii.EventRelationId__c))
sIdInforme.add(ii.EventRelationId__c);

}
System.Debug('sIdInforme Records: ' + sIdInforme);


List<Informe_icex__c> listToBeUpdated = new List<Informe_icex__c>();

for(EventRelation er:[SELECT Id,response,Status,RelationId,EventId,RespondedDate FROM EventRelation WHERE Id IN :sIdInforme]){
System.Debug('EventRelation Records er: ' + er);
if (!iMap.containsKey(er.Id)) {
System.Debug('passed through here : ' + iMap);
Informe_icex__c icex = iMap.get(er.Id); //I saw that this assignment is empty, the error is System.NullPointerException: Attempt to de-reference a null object
System.Debug('Icex Records : '+ icex);
if(er.Status!='New'){
System.Debug('Its different from new : ');
icex.Comentarios_Invitados__c = er.response;
icex.Estado_Invitado__c = er.status;
listToBeUpdated.add(icex);

}
}

}
if(listToBeUpdated.size()>0) update listToBeUpdated;
System.Debug('passed through here update : ');
}

souvik9086souvik9086

Try like this

 

global Database.Querylocator start(Database.BatchableContext bc){
//where fecha_del_evento__c>=LAST_N_DAYS:1
return Database.getQueryLocator([SELECT ID, Comentarios_Invitados__c,EventRelationId__c,Estado_Invitado__c,fecha_del_evento__c FROM Informe_icex__c]);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){

Map<ID, Informe_icex__c> iMap = new Map<ID, Informe_icex__c>((List<Informe_icex__c>)scope);
System.Debug('iMap Records : ' + iMap);

Set<Id> sIdInforme = new Set<Id>();
Informe_icex__c ii;

for(sObject c:scope) {
ii = (Informe_icex__c) c;
if(!sIdInforme.contains(ii.EventRelationId__c))
sIdInforme.add(ii.EventRelationId__c);

}
System.Debug('sIdInforme Records: ' + sIdInforme);


List<Informe_icex__c> listToBeUpdated = new List<Informe_icex__c>();

for(EventRelation er:[SELECT Id,response,Status,RelationId,EventId,RespondedDate FROM EventRelation WHERE Id IN :sIdInforme]){
System.Debug('EventRelation Records er: ' + er);
if (!iMap.containsKey(er.Id)) {
System.Debug('passed through here : ' + iMap);
if(iMap.get(er.Id) != NULL){
Informe_icex__c icex = iMap.get(er.Id);
System.Debug('Icex Records : '+ icex);
if(er.Status!='New'){
System.Debug('Its different from new : ');
icex.Comentarios_Invitados__c = er.response;
icex.Estado_Invitado__c = er.status;
listToBeUpdated.add(icex);
}
}
}

}
if(listToBeUpdated.size()>0) update listToBeUpdated;
System.Debug('passed through here update : ');
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Jesus FerrerasJesus Ferreras

ok, I have no error, but does not pass, the value of (iMap.get(er.Id) is null

asawantasawant

Jesus,

 

You may want to check if the Map actually contains the value it needs. This can be caused due to batching of records in execute(...) method. Consider if changing the Batch to implement Database.Stateful resolves the issue.

 

Thanks,

Anand

Jesus FerrerasJesus Ferreras

Hello,

 

is this the line correct

Informe_icex__c icex = iMap.get(er.Id);

not pass, because the icex is null

someone can help me, please? 

Jesus FerrerasJesus Ferreras

My apex batch has all method.

 

Thanks,

 

global class Actualizar_informe implements Database.Batchable<sObject>,Database.stateful{

global Database.Querylocator start(Database.BatchableContext bc){
//where fecha_del_evento__c>=LAST_N_DAYS:1
return Database.getQueryLocator([SELECT ID, Comentarios_Invitados__c,EventRelationId__c,Estado_Invitado__c,fecha_del_evento__c FROM Informe_icex__c]);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){

Map<ID, Informe_icex__c> iMap = new Map<ID, Informe_icex__c>((List<Informe_icex__c>)scope);
System.Debug('iMap Records : ' + iMap);

Set<Id> sIdInforme = new Set<Id>();
Informe_icex__c ii;

for(sObject c:scope) {
ii = (Informe_icex__c) c;
if(!sIdInforme.contains(ii.EventRelationId__c))
sIdInforme.add(ii.EventRelationId__c);

}
System.Debug('sIdInforme Records: ' + sIdInforme);


List<Informe_icex__c> listToBeUpdated = new List<Informe_icex__c>();

for(EventRelation er:[SELECT Id,response,Status,RelationId,EventId,RespondedDate FROM EventRelation WHERE Id IN :sIdInforme]){
System.Debug('EventRelation Records er: ' + er);
if (!iMap.containsKey(er.Id)) {
System.Debug('passed through here : ' + iMap);
System.Debug('passed through here er.Id : ' + er.Id);
System.Debug('passed through here er.response : ' + er.response);
System.Debug('passed through here er.status : ' + er.status);
if(iMap.get(er.Id) != NULL){
System.Debug('passed through here2 : ');
Informe_icex__c icex = iMap.get(er.Id);
System.Debug('Icex Records : '+ icex);
if(er.Status!='New'){
System.Debug('Its different from new : ');
icex.Comentarios_Invitados__c = er.response;
icex.Estado_Invitado__c = er.status;
listToBeUpdated.add(icex);
}
}
}
}
if(listToBeUpdated.size()>0) update listToBeUpdated;
System.Debug('passed through here update : ');
}

global void finish(Database.BatchableContext bc)
{
Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
String[]toAddresses = new String[]{'gferreras@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('the Apex Batch finished');
mail.setPlainTextBody('the Apex Batch finished');
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}

Jesus FerrerasJesus Ferreras

Hello, 

anyone can help me with this, please.
I have problems in this part of the code. I want to update the rows that come from this list, but it does not work.

 

 

 

for(EventRelation er:[SELECT Id,response,Status,RelationId,EventId,RespondedDate FROM EventRelation WHERE Id IN :sIdInforme]){
System.Debug('EventRelation Records er: ' + er);
if (!iMap.containsKey(er.Id)) {

System.Debug('passed through here : ' + iMap);
System.Debug('passed through here er.Id : ' + er.Id);
System.Debug('passed through here er.response : ' + er.response);
System.Debug('passed through here er.status : ' + er.status);
if(iMap.get(er.Id) != NULL){
System.Debug('passed through here2 : ');
Informe_icex__c icex = iMap.get(er.Id);  the problems is I cant pass here, please tell me what can i do.
System.Debug('Icex Records : '+ icex);
if(er.Status!='New'){
System.Debug('Its different from new : ');
icex.Comentarios_Invitados__c = er.response;
icex.Estado_Invitado__c = er.status;
listToBeUpdated.add(icex);
}
}
}
}
if(listToBeUpdated.size()>0) update listToBeUpdated;
System.Debug('passed through here update : ');
}

 

Thanks,

Devender MDevender M

for(EventRelation er:[SELECT Id,response,Status,RelationId,EventId,RespondedDate FROM EventRelation WHERE Id IN :sIdInforme]){
System.Debug('EventRelation Records er: ' + er);
if (!iMap.containsKey(er.Id)) {

System.Debug('passed through here : ' + iMap);
System.Debug('passed through here er.Id : ' + er.Id);
System.Debug('passed through here er.response : ' + er.response);
System.Debug('passed through here er.status : ' + er.status);
if(iMap.containsKey(er.Id) )// try this code{
System.Debug('passed through here2 : ');
Informe_icex__c icex = iMap.get(er.Id); 
System.Debug('Icex Records : '+ icex);
if(er.Status!='New'){ 
System.Debug('Its different from new : ');
icex.Comentarios_Invitados__c = er.response;
icex.Estado_Invitado__c = er.status;
listToBeUpdated.add(icex);

}
}
}
if(listToBeUpdated.size()>0) update listToBeUpdated;
System.Debug('passed through here update : ');
}