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
Santiago BunceSantiago Bunce 

De-Reference and NullPointerException Help

Hello,

I'm working to deploy from sandbox to our production environment.

When I validate the inbound changeset in our production environment, I get the following error:

System.NullPointerException: Attempt to de-reference a null object
Stack Trace: Class.MassUpdateSimpleControllerTest.testOpportunityInRelatedList: line 148, column 1

line 148 reads:         System.assert(controller.getFieldTypeOptions().size()>1);

I've done some research and have seen you can possibly comment-out the line in question, or de-reference the null so that I guess a new object is created and the code works properly (I'm not quite sure I understand this one) I've tried to edit this code to make it work, but still have errors. Can anyone help me, please?!

I have very limited coding and Salesforce experience, so any clarity and insight is appreciated!

Here is the Apex Class Code: 
/**
 * This class contains unit tests for validating the behavior of MassUpdateController
 * and triggers.
 */
@isTest
private class MassUpdateSimpleControllerTest {


    static testMethod void singleUpdateTest() {
        Opportunity o = new Opportunity();
        List<Opportunity> oppList = [SELECT name FROM Opportunity LIMIT 20];
        
        ApexPages.StandardSetController setCtr = new ApexPages.StandardSetController(oppList);
        setCtr.setSelected(new Opportunity[]{o});
        MassUpdateSimpleController controller = new MassUpdateSimpleController(setCtr);
        System.assertEquals(1, controller.getRecordSize());
        
        System.assert(controller.getFieldTypeOptions().size()>1);
        
        system.assertEquals(1, controller.objsToUpdate.size());
        
        String value = '123test';
        controller.fieldName='name';
        controller.valueToUpdate=value; 
        //controller.convertedFieldData = controller.convertUserInputToFieldData(); 
        controller.step4();
        controller.step5();
        
        System.assert(o.name==value);
        
        value ='123';
        controller.step3();
        controller.fieldName='amount';
        controller.valueToUpdate=value; 
        controller.step4();
        controller.step5();
        
        System.assert(o.amount==decimal.valueOf(value));
        
/*      value ='true';
        controller.fieldName='IsPrivate';
        controller.step3();
        controller.valueToUpdate=value;     
        controller.step4();
        controller.step5();
        
        System.assert(o.IsPrivate); */
        // make sure no exception from display tips
        System.assertEquals(controller.getFieldInfoToDisplay()!=null,true);
                
        value ='2009-4-7';
        controller.fieldName='CloseDate';
        controller.valueToUpdate=value;     
        controller.step4();
        controller.step5();
        System.assert(o.CloseDate==Date.valueOf(value));
        
        value ='Closed';
        controller.fieldName='StageName';
        controller.valueToUpdate=value;     
        controller.step4();
        controller.step5();
        System.assert(o.StageName=='Closed');
    }
    
 /*   static testMethod void massUpdateAsStandardUserTest() {
        
        Profile p = [select id from profile where name='Standard User'];
        User u = new User(alias = 'standt', email='standarduser@testorg.com',
          emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
          localesidkey='en_US', profileid = p.Id,
          timezonesidkey='America/Los_Angeles', username='standarduser@test.com');
        
        System.runAs(u) {
          Opportunity o = new Opportunity();
          List<Opportunity> oppList = [SELECT name FROM Opportunity LIMIT 20];
        
          ApexPages.StandardSetController setCtr = new ApexPages.StandardSetController(oppList);
          setCtr.setSelected(new Opportunity[]{o});
          MassUpdateSimpleController controller = new MassUpdateSimpleController(setCtr);
          System.assertEquals(1, controller.getRecordSize());
        
          System.assert(controller.getFieldTypeOptions().size()>1);
        
          system.assertEquals(1, controller.objsToUpdate.size());
        
          String value = '123test';
          controller.fieldName='name';
          controller.valueToUpdate=value;       
          controller.save();
        
          System.assert(o.name==value);
         
        }
    }  */
    
    static testMethod void linkTest() {
        Opportunity o = new Opportunity();
        List<Opportunity> oppList = [SELECT name FROM Opportunity LIMIT 20];
        
        ApexPages.StandardSetController setCtr = new ApexPages.StandardSetController(oppList);
        setCtr.setSelected(new Opportunity[]{o});
        MassUpdateSimpleController controller = new MassUpdateSimpleController(setCtr);
        
        // verify following exceptions will not cause exception
        System.assert(controller.step1()!=null);
        System.assert(controller.step2()!=null);
        controller.getFieldTypeOptions();
        System.assert(controller.step3()!=null);
        System.assert(controller.step4()!=null);
        System.assert(controller.step5()!=null);
        //System.assert(controller.cancel()!=null);
        
        System.assert(controller.getFieldTypeOptions()!=null);
    }
    
    static testMethod void fieldTest() {
        
        List<Opportunity> oppList = new Opportunity[]{};
        
        ApexPages.StandardSetController setCtr = new ApexPages.StandardSetController(oppList);
        MassUpdateSimpleController controller = new MassUpdateSimpleController(setCtr);
        System.assert(controller.cancel()!=null);
        System.assert(controller.getFieldTypeOptions()==null);
    }
    
    static testMethod void miscTest() {
        
        List<Opportunity> oppList = new Opportunity[]{};
        
        ApexPages.StandardSetController setCtr = new ApexPages.StandardSetController(oppList);
        MassUpdateSimpleController controller = new MassUpdateSimpleController(setCtr);
        
        System.assert(controller.getNow(true)!=null);
        System.assert(controller.getNow(false)!=null);
        System.assert(controller.getRecordSize()==0);
        System.assert(controller.getPicklistValues()==null);
    }
    
    @isTest(SeeAllData=true)
    static void testOpportunityInRelatedList(){
        List<Opportunity> oppList = [SELECT name, AccountId FROM Opportunity LIMIT 20];
        ApexPages.StandardSetController setCtr = new ApexPages.StandardSetController(oppList);
        apexpages.currentpage().getparameters().put('id' , oppList.get(0).AccountId);
        MassUpdateSimpleController controller = new MassUpdateSimpleController(setCtr);
        System.debug('list: ' + oppList);
        
        System.assert(controller.getFieldTypeOptions().size()>1);
        
        //system.assertEquals(1, controller.objsToUpdate.size());
        
        String value = '123test';
        controller.fieldName='name';
        controller.valueToUpdate=value; 
        //controller.convertedFieldData = controller.convertUserInputToFieldData(); 
        controller.lexstep4();
        controller.lexstep5();
        List<Opportunity> selectList = (List<Opportunity>)setCtr.getSelected();
        System.assert(selectList.get(0).name==value);
        System.assert(controller.displayTexField=='true');
        System.assert(controller.displayCheckBox=='false');
        System.assert(controller.displayPicklist=='false');
        System.assert(controller.displayTextArea=='false');
        
         value ='Closed';
        controller.lexstep3();
        controller.fieldName='StageName';
        controller.valueToUpdate=value;     
        controller.lexstep4();
        controller.lexstep5();
        System.assert(selectList.get(0).StageName=='Closed');
        System.assert(controller.displayTexField=='false');
        System.assert(controller.displayCheckBox=='false');
        System.assert(controller.displayPicklist=='true');
        System.assert(controller.displayTextArea=='false');
        
        
    }
}
Prakhar Saxena 19Prakhar Saxena 19
Hi Santiago,

I guess the error appears because of the annotation.
(SeeAllData=true)
Using the isTest(SeeAllData=true) Annotation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm)

This annotation opens up data access to records in your organization. Ideally, we should not use this annotation and instead create sample test data in the test methods and use that data to test our Apex classes.

This is mainly because the data present in your sandbox might be different from what is currently there in production. Also, it's not the best practice to give access to all org data in your test class.

Can you try removing the annotation '(SeeAllData=true)' from the method 'testOpportunityInRelatedList' and insert a sample opportunity record at the start of the method like below and see if that makes any difference?
 
Opportunity opp = new Opportunity(Name = 'Test Opportunity',
                                  StageName = 'Closed',
                                  CloseDate = Date.today());
insert opp;

 
Santiago BunceSantiago Bunce
Thank you, Prakhar. I appreciate the help.

I tried ot incorporate the new code but got an error. I removed '(SeeAllData=true)' and on the line after '@isTest' added the fourl ines of code you suggested. After 'insert opp;' I continued the code as was before with 'static void testOpportunitInRelatedList(){'  and the rest of the code continues as before. 

However, I get the following error: "Error: Compile Error: Expecting '}' but was: 'insert' at line 144 column 5". That line of code is 'insert opp;'

The code I sent was originally written by a developer who was helping us but no longer able to (we're a nonprofit and had won a grant that gave us support with writing code). 

I'm not sure if it matters, but we don't even use Opportunities in our production environment. We focus on accounts, contacts, and a few custom objects. But I don't know if opportunities interacts with another object somewhere in the back end, which is perhaps why the developer wrote this code.

Any other thoughts?

Thank you!


 @isTest
    Opportunity opp = new Opportunity(Name = 'Test Opportunity',
    StageName = 'Closed',
    CloseDate = 'Date.today()');
    insert opp;   
    static void testOpportunityInRelatedList(){
        List<Opportunity> oppList = [SELECT name, AccountId FROM Opportunity LIMIT 20];
        ApexPages.StandardSetController setCtr = new ApexPages.StandardSetController(oppList);
        apexpages.currentpage().getparameters().put('id' , oppList.get(0).AccountId);
        MassUpdateSimpleController controller = new MassUpdateSimpleController(setCtr);
        System.debug('list: ' + oppList);
        
        System.assert(controller.getFieldTypeOptions().size()>1);
Prakhar Saxena 19Prakhar Saxena 19
You can try removing the quotes ' ' from the line CloseDate = 'Date.today()'. Since it's not a String field, so no need to add quotes. It should be like this:
 
Opportunity opp = new Opportunity(Name = 'Test Opportunity',
                                  StageName = 'Closed',
                                  CloseDate = Date.today());
insert opp;
Santiago BunceSantiago Bunce
Thank you, Prakhar. I really appreciate your help. Unfortunately, that solution didn't work. It still says it's expecting '{' but receives 'insert'. I will try to figure it out.