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
CPRCPR 

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

Hello,

Here is my test class which I am getting Attempt to de-reference a null object. Not sure what's wrong. Any help to resovle this error will be appreciated. So there is VF page where users enters start date and end date with datatime data type, and based on these dates, clickout records should be fetched and printed as pdf.

Public Static TestMethod void test1(){
        init();   // this method creates the required data like opp, account, contact, etc.
           
        clickO.CLICK_OUT_DATE__C = system.now(); // error on this line "FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object"
        clickO.RELATED_MERHCANT__C = 'abcd';
        clickO.MERCHANT_OF_THE_DAY__C = true;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.33;
        insert clickO;
       
       clickO.CLICK_OUT_DATE__C = system.now();
        clickO.RELATED_MERHCANT__C = 'abcd';
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= true;
        clickO.COST_PER_CLICK__C= 0.33;
        insert clickO;
       
       clickO.CLICK_OUT_DATE__C = system.now();
        clickO.RELATED_MERHCANT__C = 'abcd';
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.33;
        insert clickO;          
            
        Test.startTest();
            ApexPages.StandardController scon = new ApexPages.StandardController(opp);
            OpportunityPDFController pdfC = new OpportunityPDFController(scon);
          
            pdfC.billing.Billing_Cycle_Start_Date__c = system.now();
            pdfC.billing.Billing_Cycle_end_Date__c = system.now().addDays(8);
            pdfc.billing.refund__c=12;
           
            pdfc.refund=2;
           
            pdfC.refreshCheckout(); // This method refresh the vf page, and display all the click out records between start date and end date
            pdfC.generatePDF();  // This method generates pdf.
           
            pdfC.generatePDFBill();
            pdfC.savePDF();
            ApexPages.currentPage().getParameters().get(pdfC.billing.id);
            pdfC.refreshCheckoutPDF();
           
        Test.stopTest();
   
    }
Eric_SalesForceEric_SalesForce
the first thing i noticed was that init() is not declared or called. For example testClass.init() is not specified. I also noticed that you are getting parameters which can often lead to null pointers. I would add System.debugs throughout the code, track how far down you get before you reach the null pointer and eliminate the error that way.
CPRCPR
Thanks for the quick reply.  As you can see that there are no null values returned by init() method.

@isTest

Public Class OpportunityPDFControllerTest{

    Static Opportunity opp;
    Static Click_Outs__c clickO;
    Static Billing_Event__c be;
    Static Account acc;
    Static Contact con;
   
    Static void init(){
        acc = new Account();
        acc.Name ='abc Account';
        insert acc;
        system.debug(acc);
       
        con = new Contact();
        con.LastName = 'abcd';
        insert con;
        system.debug(con);
       
        Location__c billingCountry = new Location__c();
        billingCountry.VAT_Percentage__c=5.0;
        billingCountry.Name ='DE';
       
        insert billingCountry;
        system.debug(billingCountry);
       
        opp= new Opportunity();
        opp.Receive_Bill__c = True;
        opp.Billing_Address__c = '123 abc address';
        opp.Account = acc;
        opp.Name='abcd';
        opp.StageName = 'Prospect' ;
        opp.CloseDate =System.today()+10;
        opp.Billing_Country__c = billingCountry.id;
       
        insert opp;
        system.debug(opp);
   
    }
   

   /* Static void createClickOut(Decimal amount,Integer clickType){
         Decimal vatPer;
        
         clickO = new Click_Outs__c();
         vatPer = opp.VATPercentage__c;
         Decimal vat = (vatPer * clickO.COST_PER_CLICK__C)/100;
         clickO.Click_Out_Date__c = System.now()+2;
         clickO.Cost_Per_Click__c = amount;
        
         if(clickType==1)
             clickO.Merchant_Of_The_Day__c=True;
         else if(clickType==2)
             clickO.Merchant_Of_The_Week__c =True;
            
         clickO.Related_Merhcant__c = opp.id;
         insert clickO;
   
    }*/
   
    Public Static TestMethod void test1(){
        init();
       
        clickO.CLICK_OUT_DATE__C = system.today().addDays(-3);
        clickO.RELATED_MERHCANT__C = 'abcd';
        clickO.MERCHANT_OF_THE_DAY__C = true;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.33;
        insert clickO;
        system.debug(clickO);
       
       clickO.CLICK_OUT_DATE__C = system.today().addDays(-2);
        clickO.RELATED_MERHCANT__C = 'abcd';
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= true;
        clickO.COST_PER_CLICK__C= 0.33;
        insert clickO;
       
       clickO.CLICK_OUT_DATE__C = system.today().addDays(-1);
        clickO.RELATED_MERHCANT__C = 'abcd';
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.33;
        insert clickO;                 
       
            
        Test.startTest();
            ApexPages.StandardController scon = new ApexPages.StandardController(opp);
            OpportunityPDFController pdfC = new OpportunityPDFController(scon);
          
            pdfC.billing.Billing_Cycle_Start_Date__c = system.today();
            pdfC.billing.Billing_Cycle_end_Date__c = system.today().addDays(8);
            pdfc.billing.refund__c=12;
           
            pdfc.refund=2;
           
            pdfC.refreshCheckout();
            pdfC.generatePDF();
           
            pdfC.generatePDFBill();
            pdfC.savePDF();
            ApexPages.currentPage().getParameters().get(pdfC.billing.id);
            pdfC.refreshCheckoutPDF();
           
        Test.stopTest();
   
    }
   
}

And here is the debug log :

29.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
19:35:01.256 (256504000)|EXECUTION_STARTED
19:35:01.256 (256549000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000004Tehc|OpportunityPDFControllerTest.test1
19:35:01.256 (256989000)|METHOD_ENTRY|[3]|01p90000004Tehc|OpportunityPDFControllerTest.OpportunityPDFControllerTest()
19:35:01.257 (257000000)|METHOD_EXIT|[3]|OpportunityPDFControllerTest
19:35:01.257 (257119000)|METHOD_ENTRY|[64]|01p90000004Tehc|OpportunityPDFControllerTest.init()
19:35:01.257 (257343000)|DML_BEGIN|[14]|Op:Insert|Type:Account|Rows:1
19:35:01.329 (329750000)|DML_END|[14]
19:35:01.329 (329811000)|SYSTEM_METHOD_ENTRY|[15]|System.debug(ANY)
19:35:01.329 (329863000)|USER_DEBUG|[15]|DEBUG|Account:{Name=abc Account, Id=0019000000tShg8AAC}
19:35:01.329 (329871000)|SYSTEM_METHOD_EXIT|[15]|System.debug(ANY)
19:35:01.330 (330041000)|DML_BEGIN|[19]|Op:Insert|Type:Contact|Rows:1
19:35:01.382 (382311000)|DML_END|[19]
19:35:01.382 (382364000)|SYSTEM_METHOD_ENTRY|[20]|System.debug(ANY)
19:35:01.382 (382414000)|USER_DEBUG|[20]|DEBUG|Contact:{Id=0039000000uruskAAA, LastName=abcd}
19:35:01.382 (382426000)|SYSTEM_METHOD_EXIT|[20]|System.debug(ANY)
19:35:01.382 (382668000)|DML_BEGIN|[26]|Op:Insert|Type:Location__c|Rows:1
19:35:01.424 (424892000)|DML_END|[26]
19:35:01.424 (424938000)|SYSTEM_METHOD_ENTRY|[27]|System.debug(ANY)
19:35:01.424 (424992000)|USER_DEBUG|[27]|DEBUG|Location__c:{Name=DE, VAT_Percentage__c=5.0, Id=a009000000gfiQZAAY}
19:35:01.425 (425001000)|SYSTEM_METHOD_EXIT|[27]|System.debug(ANY)
19:35:01.425 (425225000)|SYSTEM_METHOD_ENTRY|[35]|System.today()
19:35:01.425 (425254000)|SYSTEM_METHOD_EXIT|[35]|System.today()
19:35:01.425 (425285000)|SYSTEM_METHOD_ENTRY|[35]|Date.addDays(Integer)
19:35:01.425 (425298000)|SYSTEM_METHOD_EXIT|[35]|Date.addDays(Integer)
19:35:01.425 (425396000)|DML_BEGIN|[38]|Op:Insert|Type:Opportunity|Rows:1
19:35:01.452 (452037000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Opportunity:new
19:35:01.452 (452054000)|VALIDATION_RULE|03d90000000c61m|Mandate_Billing_Address
19:35:01.452 (452162000)|VALIDATION_FORMULA|AND(ISBLANK( Billing_Address__c ),ISPICKVAL(StageName,'Closed Won'))|StageName=Prospect , Billing_Address__c=123 abc address
19:35:01.452 (452170000)|VALIDATION_PASS
19:35:01.452 (452172000)|VALIDATION_RULE|03d90000000c48v|Mandate_Trial_Period_Fields
19:35:01.452 (452257000)|VALIDATION_FORMULA|AND(ISPICKVAL(StageName,'Trial Period'), ISBLANK(Trial_Start_Date__c),ISBLANK( Trail_End_Date__c ))|StageName=Prospect , Trail_End_Date__c=null , Trial_Start_Date__c=null
19:35:01.452 (452264000)|VALIDATION_PASS
19:35:01.452 (452267000)|CODE_UNIT_FINISHED|Validation:Opportunity:new
19:35:01.583 (583103000)|DML_END|[38]
19:35:01.583 (583166000)|SYSTEM_METHOD_ENTRY|[39]|System.debug(ANY)
19:35:01.583 (583253000)|USER_DEBUG|[39]|DEBUG|Opportunity:{Name=abcd, Billing_Address__c=123 abc address, StageName=Prospect, Billing_Country__c=a009000000gfiQZAAY, Receive_Bill__c=true, Id=0069000000IDKIVAA5, CloseDate=2014-04-27 00:00:00}
19:35:01.583 (583264000)|SYSTEM_METHOD_EXIT|[39]|System.debug(ANY)
19:35:01.583 (583276000)|METHOD_EXIT|[64]|01p90000004Tehc|OpportunityPDFControllerTest.init()
19:35:01.583 (583291000)|SYSTEM_METHOD_ENTRY|[66]|System.today()
19:35:01.583 (583316000)|SYSTEM_METHOD_EXIT|[66]|System.today()
19:35:01.583 (583349000)|SYSTEM_METHOD_ENTRY|[66]|Date.addDays(Integer)
19:35:01.583 (583364000)|SYSTEM_METHOD_EXIT|[66]|Date.addDays(Integer)
19:35:01.583 (583497000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.OpportunityPDFControllerTest.test1: line 66, column 1
19:35:01.583 (583512000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.OpportunityPDFControllerTest.test1: line 66, column 1
19:35:01.412 (583529000)|CUMULATIVE_LIMIT_USAGE
19:35:01.412|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 4 out of 150
  Number of DML rows: 4 out of 10000
  Number of code statements: 0 out of 200000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

19:35:01.412|CUMULATIVE_LIMIT_USAGE_END

19:35:01.583 (583558000)|CODE_UNIT_FINISHED|OpportunityPDFControllerTest.test1
19:35:01.583 (583564000)|EXECUTION_FINISHED
Eric_SalesForceEric_SalesForce
At a quick glance, ~ line 66 is click0.click_out_date__c you have set click0 at line 48 in a separate method and I do not believe that click0 has been defined for the test1 method I would look there. Also when you are debugging filter it by debug logs by using the checkbox at the bottom. Hope this helps.
CPRCPR
Thanks a lot for your time and responding to my query. Now I am facing some other error "FATAL_ERROR|System.ListException: Before Insert or Upsert list must not have two identically equal elements"

@isTest

Public Class OpportunityPDFControllerTest{

    Static Opportunity opp;
 
    Static Billing_Event__c be;
    Static Account acc;
    Static Contact con;
   
    Static void init(){
        acc = new Account();
        acc.Name ='abc Account';
        insert acc;
        system.debug(acc);
       
        con = new Contact();
        con.LastName = 'abcd';
        insert con;
        system.debug(con);
       
        Location__c billingCountry = new Location__c();
        billingCountry.VAT_Percentage__c=5.0;
        billingCountry.Name ='DE';
       
        insert billingCountry;
        system.debug(billingCountry);
       
        opp= new Opportunity();
        opp.Receive_Bill__c = True;
        opp.Billing_Address__c = '123 abc address';
        opp.Account = acc;
        opp.Name='abcd';
        opp.StageName = 'Prospect' ;
        opp.CloseDate =System.today()+10;
        opp.Billing_Country__c = billingCountry.id;
       
        insert opp;
        system.debug(opp);
   
    }
   
   Static void createClickOut(){
          Click_Outs__c clickO = new Click_Outs__c();
        List<Click_Outs__c> clickOList = new List<Click_Outs__c>();
       
        clickO.CLICK_OUT_DATE__C = system.today().addDays(-3);
        clickO.RELATED_MERHCANT__C = opp.id;
        clickO.MERCHANT_OF_THE_DAY__C = true;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.33;
        clickOList.add(clickO);
        system.debug(clickO);
       
       clickO.CLICK_OUT_DATE__C = system.today().addDays(-2);
        clickO.RELATED_MERHCANT__C = opp.id;
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= true;
        clickO.COST_PER_CLICK__C= 0.30;
        clickOList.add(clickO);
        system.debug(clickO);
       
        clickO.CLICK_OUT_DATE__C = system.today().addDays(-1);
        clickO.RELATED_MERHCANT__C = opp.id;
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.34;
        clickOList.add(clickO);        
        system.debug(clickOList);    
       
        insert clickOList; // Error Line : FATAL_ERROR|System.ListException: Before Insert or Upsert list must not have two identically equal elements
        }

   /* Static void createClickOut(Decimal amount,Integer clickType){
         Decimal vatPer;
        
         clickO = new Click_Outs__c();
         vatPer = opp.VATPercentage__c;
         Decimal vat = (vatPer * clickO.COST_PER_CLICK__C)/100;
         clickO.Click_Out_Date__c = System.now()+2;
         clickO.Cost_Per_Click__c = amount;
        
         if(clickType==1)
             clickO.Merchant_Of_The_Day__c=True;
         else if(clickType==2)
             clickO.Merchant_Of_The_Week__c =True;
            
         clickO.Related_Merhcant__c = opp.id;
         insert clickO;
   
    }*/
   
    Public Static TestMethod void test1(){
        init();  
        createClickOut();
                   
        Test.startTest();
            ApexPages.StandardController scon = new ApexPages.StandardController(opp);
            OpportunityPDFController pdfC = new OpportunityPDFController(scon);
          
            pdfC.billing.Billing_Cycle_Start_Date__c = system.today();
            pdfC.billing.Billing_Cycle_end_Date__c = system.today().addDays(8);
            pdfc.billing.refund__c=12;
           
            pdfc.refund=2;
           
            pdfC.refreshCheckout();
            pdfC.generatePDF();
           
            pdfC.generatePDFBill();
            pdfC.savePDF();
            ApexPages.currentPage().getParameters().get(pdfC.billing.id);
            pdfC.refreshCheckoutPDF();
           
        Test.stopTest();
   
    }
   
}