1

I am getting below error while running the below test class -

System.NullPointerException: Attempt to de-reference a null object at 
Class.SmartAccountDetailsExtension_Test.testSmartAccountDetailsExtension: line 11, column 1

Class -

public class SmartAccountDetailsExtension {

   private Smart_Account__C smartaccount;
   private ApexPages.StandardController stdController ;
public SmartAccountDetailsExtension(ApexPages.StandardController controller){
        this.smartaccount= (Smart_Account__C)stdController.getRecord();
        this.stdController = stdController;///This line is not getting civered in test class
    } // End Method

 }

Test Class -

@isTest(SeeAllData=true)
public class SmartAccountDetailsExtension_Test {

    static ApexPages.StandardController controller;
    Static List<Smart_Account__c> sm;

    public static testMethod void testSmartAccountDetailsExtension(){

        sm=[SELECT id, Name FROM Smart_Account__c limit 1];
        controller = new ApexPages.StandardController(sm[0]);
        SmartAccountDetailsExtension sad = new SmartAccountDetailsExtension(controller);
    }
}

2 Answers 2

3

The problem is not in the test but in you're constructor: In your constructor, you're referring to stdController.getRecord() but the value (the crtl value) you're passing to the constructor is called just controller:

public SmartAccountDetailsExtension(ApexPages.StandardController **controller**){

So you need to replace stdController.getRecord() with controller.getRecord(), and replace this: this.stdController = stdController; with this.stdController = controller;

Good luck!

0

You haven't initialized you list

@isTest(SeeAllData=true)
public class SmartAccountDetailsExtension_Test {

    static ApexPages.StandardController controller;
    Static List<Smart_Account__c> sm;

    public static testMethod void testSmartAccountDetailsExtension(){
        sm = List<Smart_Account__c>();
        sm=[SELECT id, Name FROM Smart_Account__c limit 1];
        controller = new ApexPages.StandardController(sm[0]);
        SmartAccountDetailsExtension sad = new SmartAccountDetailsExtension(controller);
    }
}

Apex Class:

public class SmartAccountDetailsExtension {

   private Smart_Account__C smartaccount;
   private ApexPages.StandardController stdController ;
public SmartAccountDetailsExtension(ApexPages.StandardController controller){
        this.smartaccount= (Smart_Account__C)controller.getRecord();
        this.stdController = controller;///This line is not getting civered in test class
    } // End Method

 }

As best practice you should create test data inside your test class instead of using SeeAllData=true

4
  • still it is showing error in this '''SmartAccountDetailsExtension sad = new SmartAccountDetailsExtension(controller);'''' line Commented Aug 7, 2016 at 11:48
  • what is error. Please modify your question with error.
    – Himanshu
    Commented Aug 7, 2016 at 11:51
  • I have modified my question wth error Commented Aug 7, 2016 at 11:56
  • 1
    i have updated code. as Yonantan mentioned you are using stdController which you have not initialized so use controller which you are passing in constructor.
    – Himanshu
    Commented Aug 7, 2016 at 12:05

You must log in to answer this question.

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