Reply
Contributor
gopivyas`
Posts: 8
Accepted Solution

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

Hi All,
 
I am getting follwoing errors while doing the batch upload on opportunity obj.
 
Developer script exception from DuplicatelineOpptychange : DuplicatelineOpptychange: execution of BeforeUpdate  caused by: System.NullPointerException: Attempt to de-reference a null object  Class.DuplicatelineOpptyUpdate.opptyupdate: line 134, column 99
 
Here is my code:
 
public static  Opportunity[] opptyupdate(Opportunity[] opptyIds){

  System.debug('In opptyupdate public method of the UpdateOpptypkg class');
  System.debug('Processing ' + opptyIds.size() + ' opportunities');

//Get the partners name related to the changed opportunity
OpportunityPartner[] partner = [select AccountTo.Name, OpportunityId From OpportunityPartner WHERE OpportunityId in :opptyIds ];

//Get the Tech & Service name related to the changed oppty
OpportunityLineItem[] technologyentry = [Select PricebookEntry.ProductCode, PricebookEntry.Name, Mix__c , OpportunityId From OpportunityLineItem  where OpportunityId in: opptyIds order by Mix__c DESC];


Integer x1 =0;
Integer y1 =0;

for(x1=0;x1< opptyIds.size();x1++)
{
   String newPartnerString ='';

    for(y1=0; y1<partner.size();y1++)
     {
//THIS LOOP WILL FIND ALL THE PARTNERS RELATED TO THE EXISTING CHANGED OPPTY AND UPDATE THE PARTNER MERGE FIELD
if(partner[y1].OpportunityId == opptyIds[x1].Id && partner[y1].AccountToId != opptyIds[x1].AccountId && (opptyIds[x1].RecordTypeId == '0123000000003ykAAA' || opptyIds[x1].RecordTypeId == '012300000000CDLAA2') )
       
        {
                   if(newPartnerString == '')
                          {  newPartnerString += partner[y1].AccountTo.Name ;
                          }
                                         
                      else

                         { newPartnerString = newPartnerString + ';' + partner[y1].AccountTo.Name;
                         }
                                  
                 }

                   }                       
                   opptyIds[x1].Partners_Merge_Field__c = newPartnerString; //Set the new value of the merge field

       

String newservice1 ='';
String newservice2 ='';
    
       Integer q;

            for(q=0; q<technologyentry.size();q++)

//THIS LOOP WILL FIND ALL THE TECHNOLOGY AND SERVICE RELATED TO THE EXISTING CHANGED OPPTY AND UPDATE THE TECHNOLOGY AND SERVICE MERGE FIELD

    {
      if(technologyentry[q].OpportunityId == opptyIds[x1].Id )
         {
       
        if (technologyentry[q].PricebookEntry.ProductCode == 'Technology'&& (opptyIds[x1].RecordTypeId == '0123000000003ykAAA' || opptyIds[x1].RecordTypeId == '012300000000CDLAA2'))
            
             {    if(newservice1 == '')
                                          
                 { newservice1 += technologyentry[q].PricebookEntry.Name + ' (' + technologyentry[q].Mix__c.intValue()+ ')';
                 }
            else
                 { newservice1 = newservice1 + ';' + technologyentry[q].PricebookEntry.Name + ' (' + technologyentry[q].Mix__c.intValue() + ')';
                 }
              }

                  opptyIds[x1].Technology_Merge_Field__c = newservice1; //Set the new value of the merge field
              
                                                
          if (technologyentry[q].PricebookEntry.ProductCode == 'Service'&& (opptyIds[x1].RecordTypeId == '0123000000003ykAAA' || opptyIds[x1].RecordTypeId == '012300000000CDLAA2'))
                               
              {   if(newservice2 == '')
                      
                   { newservice2 += technologyentry[q].PricebookEntry.Name + ' (' + technologyentry[q].Mix__c.intValue() + ')';
                   }
             else
                   { newservice2 = newservice2 + ';' + technologyentry[q].PricebookEntry.Name + ' (' + technologyentry[q].Mix__c.intValue() + ')';
                   }

              }  
                 opptyIds[x1].Service_Merge_Field__c = newservice2; //Set the new value of the merge field
            
                               
             } //if for opptyid comparision with tech & service (opptyid)        

        }   //for tech loop      
 
           
    }//for loop for oppty
   
   
    Return  opptyIds ;
 
       
     }
   
  }
 
 
Can any one suggest why this is happening. This is really urgent to be solved. Any help would be appericiated.
 
Thanks,
 
Moderator
DevAngel
Posts: 3,214

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

Hi gopivyas,

Well, that's a lot of code. Somewhere in there you are making an assumption about the existence of a record or a field and that assumption is incorrect. You will need to make sure that the values you are working with are not null. Sometimes this can happen if you are operating on a field from a record returned from a SOQL statement, but did not include the field in the select list. There are many other reasons why this would happen as well. I would check any value that you are evaluating to ensure that it is not null.


Cheers
Dave
Developer Evangelism
BLOG - blog.sforce.com

Follow me on twitter @dcarroll
Visitor
gApps
Posts: 1

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

[ Edited ]
In your intial query, you should also exclude null values in the fields. Something like this:

OpportunityLineItem[] technologyentry = [Select PricebookEntry.ProductCode, PricebookEntry.Name, Mix__c , OpportunityId From OpportunityLineItem  where OpportunityId in: opptyIds

and <possible null field> != null

 order by Mix__c DESC];

Also, if/when you use relationships in your query, do it once for the parent and once for the child.


Message Edited by gApps on 06-24-2008 09:47 AM

Message Edited by gApps on 06-24-2008 09:47 AM
Trusted Contributor
zindalabhinav
Posts: 178

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

[ Edited ]

Thanks gApps,

 

Its really a good catch, I was querying a null field in "QueryLocator start()", so it failed. 

 

Covered this in a blog post too.