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
Robert Davis 16Robert Davis 16 

System.NullPointerException: Attempt to de-reference a null object - Update value on Contacts with Account Ids

Thank you in advance
I am attempting to pull the Account Ids from a custom object. Once I have the Account Ids I am attempting to pull all the Contacts associated with the Accounts and update a Account_Relationship_Status__c field, but continue to receive the following error: System.NullPointerException: Attempt to de-reference a null object

I have placed System.debug statements in the code in hopes to figure it out, but that has been unsuccessful. Can someone tell me what I seem to be missing?
 
Public List<Contact> contList;

for(Red_Zone__c rzAcct: [SELECT Account__c, status__c, Escalation__c FROM Red_Zone__c WHERE status__c ='Open' and Escalation__c ='Red Zone']){
    if (rzAcct.Account__c != Null && rzAcct.status__c != Null && rzAcct.Escalation__c != Null){
            System.debug('rzAcct.id '+rzAcct.id);
            System.debug('rzAcct.Account__c '+rzAcct.Account__c);
            System.debug('rzAcct.Status__c '+rzAcct.status__c);
            System.debug('rzAcct.Escalation__c '+rzAcct.Escalation__c);
        for (Contact con: [SELECT account_relationship_status__c, AccountId FROM Contact WHERE AccountId =:rzAcct.Account__c]){
            System.debug('con.id '+con.id);
            System.debug('con.account_relationship_status__c '+con.account_relationship_status__c);
            System.debug('con.AccountID '+con.AccountId);
            
               con.account_relationship_status__c ='Red Zone';
               contList.add(con);
        }
   }
System.debug('contList '+ contList);
Update contList;    
}

Log results of the Debug Statements:
Debug Log
Thank you in advance.
Balayesu ChilakalapudiBalayesu Chilakalapudi
Change your query at line 09 (inside for loop)  like this,
 
SELECT account_relationship_status__c, AccountId FROM Contact WHERE AccountId =:rzAcct.Account__c AND account_relationship_status__c !=NULL

Let us know if it helps
Robert Davis 16Robert Davis 16
Bala,

Thanks for the help, but unfortunately that would not work, because the value in the Contact Object field would have an account_relationship_status__c default value of null until this updates it. The thought is we have an object that references the account called Red_Zone__c that we complete when we are working through issues with the customer. So when we are working through issues we want to update the Contact Object to indicate that the customer is working through issues with us. So by default the account_relationship_status__c field would be null.

Robert
Robert Davis 16Robert Davis 16
LIST<Contact> contList = new List<Contact>();

for(Red_Zone__c rzAcct: [SELECT Account__c, status__c, Escalation__c FROM Red_Zone__c WHERE status__c ='Open' and Escalation__c ='Red Zone']){
    if (rzAcct.Account__c != Null && rzAcct.status__c != Null && rzAcct.Escalation__c != Null){
            System.debug('rzAcct.id '+rzAcct.id);
            System.debug('rzAcct.Account__c '+rzAcct.Account__c);
            System.debug('rzAcct.Status__c '+rzAcct.status__c);
            System.debug('rzAcct.Escalation__c '+rzAcct.Escalation__c);
        for (Contact con: [SELECT  account_relationship_status__c, AccountId FROM Contact WHERE AccountId =:rzAcct.Account__c]){
            System.debug('con.id '+con.id);
            System.debug('con.account_relationship_status__c '+con.account_relationship_status__c);
            System.debug('con.AccountID '+con.AccountId);
            
               con.account_relationship_status__c ='Red Zone';
               contList.add(con);
        }
   }
System.debug('contList '+ contList);
Update contList;    
}

The problem was that I never instaitated the variable contList at the begining, so it did not exist and I was trying to update something that does not exist:

 
Robert Davis 16Robert Davis 16
Warning the above gave me a System.LimitException: Apex CPU time limit exceeded. So will have to think of a different way.
Robert Davis 16Robert Davis 16
Ended up taking the loops from within each other it took too much CPU time as you can see by the above error. By separating the two loops it runs a lot quicker.
 
LIST<Contact> contList = new List<Contact>();
Set<ID> acctRZId = new Set<ID>();
for(Red_Zone__c rzAcct: [SELECT Account__c, status__c, Escalation__c FROM Red_Zone__c WHERE status__c ='Open' and Escalation__c ='Red Zone']){
    if (rzAcct.Account__c != Null && rzAcct.status__c != Null && rzAcct.Escalation__c != Null){
            System.debug('rzAcct.id '+rzAcct.id);
            System.debug('rzAcct.Account__c '+rzAcct.Account__c);
            System.debug('rzAcct.Status__c '+rzAcct.status__c);
            System.debug('rzAcct.Escalation__c '+rzAcct.Escalation__c);
            acctRZID.add(rzAcct.Account__c);
    }
}
for (Contact con: [SELECT  account_relationship_status__c, AccountId FROM Contact WHERE AccountId =:acctRZID]){
            System.debug('con.id '+con.id);
            System.debug('con.account_relationship_status__c '+con.account_relationship_status__c);
            System.debug('con.AccountID '+con.AccountId);
            
            con.account_relationship_status__c ='Red Zone';
            contList.add(con);
}
   
System.debug('contList '+ contList);
Update contList;

Hope this helps someone