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
BroncoBoyBroncoBoy 

Batch Process Attempt to de-reference a null object

Not sure why I 'm getting a null pointer error here in the batch process, any thoughts are appreciated :)

Code
public Database.QueryLocator start(Database.BatchableContext BC)  
{
    String query = 'SELECT Id,  (SELECT ContactId, IsPrimary FROM OpportunityContactRoles ORDER BY IsPrimary DESC) FROM Opportunity WHERE LastModifiedDate = LAST_N_DAYS:365';
    return Database.getQueryLocator(query); 
}

public void execute(Database.BatchableContext BC, List < sObject > s)
{
    List < Opportunity > oppsToUpdate = new List < Opportunity > ();
    for (sObject ssss: s)
    {
        Opportunity o = (Opportunity) ssss;
        List < OpportunityContactRole > opptys = new List < OpportunityContactRole > ();
        opptys = (List < OpportunityContactRole > ) o.getsobjects('OpportunityContactRoles'); //o.getsobjects will return a List<SObject>, in the same line we cast those into List<OpportunityContactRole>

        for (OpportunityContactRole optt: opptys) // HERE IS WHERE THE ERROR IS.  LOOPING THROUGH SUB QUERY LIST
        {
            String contId = '';
            contId = String.ValueOf(optt.ContactId);
            if (!String.IsBlank(contId))
            {
                o.Primary_Contact_Id__c = contId;
                oppsToUpdate.add(o);
                break;
            }
        }
    }

    update oppsToUpdate;

}
Best Answer chosen by BroncoBoy
suresh sanneboina 4suresh sanneboina 4
Hi,

The opportunity doesnot have the OpportunityContactRole. Try the below that might work
public Database.QueryLocator start(Database.BatchableContext BC)  
{
    String query = 'SELECT Id,  (SELECT ContactId, IsPrimary FROM OpportunityContactRoles ORDER BY IsPrimary DESC) FROM Opportunity WHERE LastModifiedDate = LAST_N_DAYS:365';
    return Database.getQueryLocator(query); 
}

public void execute(Database.BatchableContext BC, List < sObject > s)
{
    List < Opportunity > oppsToUpdate = new List < Opportunity > ();
    for (sObject ssss: s)
    {
        Opportunity o = (Opportunity) ssss;
        List < OpportunityContactRole > opptys = new List < OpportunityContactRole > ();
		if(o.OpportunityContactRoles.size() >0)
		{
			opptys = (List < OpportunityContactRole > ) o.getsobjects('OpportunityContactRoles'); //o.getsobjects will return a List<SObject>, in the same line we cast those into List<OpportunityContactRole>
			for (OpportunityContactRole optt: opptys) // HERE IS WHERE THE ERROR IS.  LOOPING THROUGH SUB QUERY LIST
			{
				String contId = '';
				contId = String.ValueOf(optt.ContactId);
				if (!String.IsBlank(contId))
				{
					o.Primary_Contact_Id__c = contId;
					oppsToUpdate.add(o);
					break;
				}
			}
		}
    }

    update oppsToUpdate;

}

All Answers

suresh sanneboina 4suresh sanneboina 4
Hi,

The opportunity doesnot have the OpportunityContactRole. Try the below that might work
public Database.QueryLocator start(Database.BatchableContext BC)  
{
    String query = 'SELECT Id,  (SELECT ContactId, IsPrimary FROM OpportunityContactRoles ORDER BY IsPrimary DESC) FROM Opportunity WHERE LastModifiedDate = LAST_N_DAYS:365';
    return Database.getQueryLocator(query); 
}

public void execute(Database.BatchableContext BC, List < sObject > s)
{
    List < Opportunity > oppsToUpdate = new List < Opportunity > ();
    for (sObject ssss: s)
    {
        Opportunity o = (Opportunity) ssss;
        List < OpportunityContactRole > opptys = new List < OpportunityContactRole > ();
		if(o.OpportunityContactRoles.size() >0)
		{
			opptys = (List < OpportunityContactRole > ) o.getsobjects('OpportunityContactRoles'); //o.getsobjects will return a List<SObject>, in the same line we cast those into List<OpportunityContactRole>
			for (OpportunityContactRole optt: opptys) // HERE IS WHERE THE ERROR IS.  LOOPING THROUGH SUB QUERY LIST
			{
				String contId = '';
				contId = String.ValueOf(optt.ContactId);
				if (!String.IsBlank(contId))
				{
					o.Primary_Contact_Id__c = contId;
					oppsToUpdate.add(o);
					break;
				}
			}
		}
    }

    update oppsToUpdate;

}
This was selected as the best answer
BroncoBoyBroncoBoy
That was it.  Thank you.  One last question:  opptys List is instantiated...I know there are no records by why does it return null?
suresh sanneboina 4suresh sanneboina 4
optysis just instantiated. but not added any records. since that doesn't entry the if block the records might not have addded.