I have a requirement where I need to compare Child object records of one parent and update values based on that. Parent Record -> Contact
Child Record 1 -> Driver (has lookup to contact)
Child Record 2 -> Vehicle (has lookup to contact)
I have to update matched__c field on Driver__c
object, if Vehicle State__c
matches with Driver State__c
When I am trying to get map value it is throwing
Null pointer error Attempt to de-reference a null object.
Using below logic to perform update:
Set<Id> ConIds = new Set<ID>();
for(Driver__c d : [Select Id, Contact__c,Status__c, Matched__c from Driver__c where Status__c ='Active']){
ConIds.add(d.Contact__c);
}
Map<Id, Vehicle__c> mapStateVehicle = new Map<ID,Vehicle__c>([Select ID, Vstate__c, Contact__c from Vehicle__c where Contact__c IN: ConIds]);
for(Driver__c d : [select Id, Name, dState__c, Contact__c from Driver__c where Name like 'XYZ%']){
if(d.dState__c == 'IL' && mapStateVehicle.get(d.Contact__c).Vstate__c == 'IL' ) // mapStateVehicle.get(d.Contact__c).Vstate__c receiving null pointer expcetion
{
Matched__c = true;
}
}
mapStateVehicle.containsKey(d.Contact__c)
mapStateVehicle.containsKey(d.Contact__c)
false means your key is not found in a map, thus meansmapStateVehicle.get(d.Contact__c)
is null and the error is because you are performing a dot operation against a null value.