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
hramanihramani 

NullPointerException on If condition


In the below code, in the second line, I get an error System.NullPointerException: Attempt to de-reference a null object
Please help.

for (Application_Review__c ar : Trigger.new) {
            if (String.isNotBlank(arts.get(ar.Application_Review_Type__c).iPad_Configuration__c)) {
                reviewIds.add(ar.Id);
            }
        }
Deepali KulshresthaDeepali Kulshrestha
Hi hramani,
Greetings to you!
 
- Please use the below code [Solved] : - 
for (Application_Review__c ar : Trigger.new) {
    if(ar.Application_Review_Type__c!=null){
        if (String.isNotBlank(arts.get(ar.Application_Review_Type__c).iPad_Configuration__c)) {
            reviewIds.add(ar.Id);
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Piyush Gautam 6Piyush Gautam 6
Hi,

There might be a possibility that the key i.e. ar.Application_Review_Type__c not present in 'arts'.
Debug the value of key before the If statement and also check whether that key is present in the map or not.

Thanks
Ajay K DubediAjay K Dubedi
Hi Hramani,
You need to add null check conditions when you fill data in map arts and also check that arts map contains Application_Review_Type__c or not.
Try this code:
for (Application_Review__c ar : Trigger.new) {
    if(ar.Application_Review_Type__c != null && arts.containsKey(ar.Application_Review_Type__c)) {
        if (String.isNotBlank(arts.get(ar.Application_Review_Type__c).iPad_Configuration__c)) {
            reviewIds.add(ar.Id);
        }
    }    
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi