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
vijayabhaskarareddyvijayabhaskarareddy 

attempt to de-reference a null object in salesforce

i  have been getting above error when i execute this code in develooper console 
 
List<orderItem> membs1 = [SELECT id FROM OrderItem WHERE orderid ='8017F000000CfJZ'];
  list <id> opids;
         
           
            for( orderItem OI : membs1)
            {
                opids.add(OI.id); 
             }
             system.debug('orde product ids r =='+ opids);

 
Best Answer chosen by vijayabhaskarareddy
Misbah ul ImanMisbah ul Iman
list <id> opids = new List<Id>();

Your List is not iniliatlized !.

All Answers

Suraj TripathiSuraj Tripathi

Hi vijayabhaskarareddy,

Try Initializing the List before. It is working fine in my Org.

 
List<orderItem> membs1 = new List <orderItem>();
membs1 = [SELECT id FROM OrderItem WHERE orderid ='8017F000000CfJZ'];
  list <id> opids;
         
           
            for( orderItem OI : membs1)
            {
                opids.add(OI.id); 
             }
             system.debug('orde product ids r =='+ opids);
Screenshot :
User-added image
I have no such record with that Id, that's why it is showing null.

Hope it Helps you. Please mark this as solved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Regards ,
Suraj
Misbah ul ImanMisbah ul Iman
list <id> opids = new List<Id>();

Your List is not iniliatlized !.
This was selected as the best answer