I have below controller where I get Attempt to dereference a null object error at ProdIds.add(c.id); Line.

Error Code

public TestController(ApexPages.StandardController stdCtrl) 
    {   redirectUrl ='';
    templateToVfpageMap = new Map<id,String>();
    TempaltesMap = new Map<id,Form_templates_abv__c>(); 
        for(Form_templates_abv__c template :[select id, Name from Form_templates_abv__c where Active_abv__c = true])
        productMap = new Map<id,product_vod__c>(); 
        for(product_vod__c product :[SELECT  Id, Name,Dosage_Strength_abv__c,Established_Name_abv__c, Proprietary_Name_abv__c,NDC_Number_abt__c,Parent_Product_vod__r.name from Product_vod__c where Product_type_vod__c ='BRC' and Active_abv__c = 'Yes' and 
                                     Id in :ProdIds])
       selectedUserAvailability = '0';  

       for(My_Setup_Products_vod__c  ac: [Select Id,Product_vod__r.Name,Ownerid,Name From My_Setup_Products_vod__c where Ownerid=:Userinfo.getUserId()])

           for(Product_vod__c c: [Select Id,Name From Product_vod__c where Parent_Product_vod__c=:ac.Product_vod__c ])      


       this.Test =   (Form_Requests_abv__c)stdCtrl.getRecord();
  • I can't see you have initialized ProdIds in your constructor
    – Ratan Paul
    Feb 18 '16 at 11:11
  • Go to the User record that you are testing under and check the "Development Mode" checkbox. Repeat the error and the stacktrace should appear giving you the exact line number that the problem occurs at and so hopefully telling you which variable is null. Then check that that variable has been initialised (something assigned to it) before it is used at that line.
    – Keith C
    Feb 18 '16 at 11:21
  • The problem is on this line. ProdIds.add(c.ID); Feb 18 '16 at 11:30
  • @SFDC_Learner I think you need to initialize your variable in controller like set<Id> ProdIds = new set<Id>();
    – Ratan Paul
    Feb 18 '16 at 11:31
  • 1
    As an aside; you have nested select statements which is prone to failure due to the limits imposed by Salesforce. Investigate bulkification patterns and see if you can refactor your code. Feb 18 '16 at 11:33

Initialize the ProdIds before it use.It will work fine.

  • This post would be better with a code snippet.
    – Adrian Larson
    Feb 18 '16 at 16:02
  • 1
    ProdIds = new Set<Id>(); Feb 19 '16 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.