1

1) This method "ConverttoInventory" is called via Custom Button on Page Layout.

2) The following below is a helper class whose static method "ConverttoInventory" is called with "Merchandise Id" as parameter.

3) This method will create a inventory record (Inventory__c).

global class MerchandiseInventoryClass {


   webservice static void ConverttoInventory(String merchandise_id)
   {
       //Convert Merchandise collection into Inventory Collection...

       Merchandise__c[] merchandise_collection = [SELECT ID,Author__c,Book_Title__c,Edition__c,Publisher__c,Total_Merchandise_QtyTotal_Qty__c FROM Merchandise__c WHERE ID = :merchandise_id];


       Inventory__c i; 
       List<Inventory__c> newInventory;
       for(Merchandise__c m : merchandise_collection )
       {
           i.Author__c = m.Author__c ; //Error occuring here..Line 15
           i.Book_Title__c = m.Book_Title__c;
           i.Edition__c = m.Edition__c;
           i.Publisher__c = m.Publisher__c;
           i.Qty_Available__c = m.Total_Merchandise_QtyTotal_Qty__c;
           newInventory.add(i);
       }
       insert newInventory;
   }

}

Upon clicking the button I am getting the following error.

enter image description here

I assumed that in Apex all objects which are not explicitly instantiated are instantiated with NULL.

Then what could be causing this error ?

Because I agree "i" is intially NULL, but at Line 15 it's "Author__c" will be set with "m.Author__c".

Then why am I getting "dereferencing a null object error".

Can someone explain ?

1 Answer 1

3

you never initialized the inventory or new inventory object. Until initialized they are null thus there are not filed to set as it is just a placeholder at that point.

Inventory__c i = new inventory__c();
List<Inventory__c> newInventory = new List<Inventory__c>();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .