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
Laurent LemazurierLaurent Lemazurier 

[APEX] System.NullPointerException: Attempt to de-reference a null object

HI,

 

I'm trying to create a button from account page, to let the user create an opportunity linked to this account.

 

Here is the code I wrote to do that. 

Notice, I'm a beginner and I've just copy an existing code wich working well.

The code works but an error occurs while exeuting test methods:

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

on this line 

PageReference p = new PageReference(Constants.RECORD_TYPE_URL);

public with sharing class New_Opportunity_On_Account
{

  private string AccountId;
     
  
  public New_Opportunity_On_Account()
  {
      AccountId = ApexPages.currentPage().getParameters().get('id');
  }

  public Pagereference init()
  {        
    PageReference p = new PageReference(Constants.RECORD_TYPE_URL);
    p.getParameters().put('ent','Opportunity');
    p.getParameters().put(constants.RET_URL,'/'+AccountId); 
    string save_Url = Constants.OPPORTUNITY_EDIT_URL + '?nooverride=1&';
    
    save_Url +=  constants.ACCOUNT_ID_FIELD_ID + '=' + AccountId  + '&' +
        constants.CLOSED_DATE_FIELD_ID +'=' +constants.CLOSED_DATE + '&' +  
        constants.RET_URL + '=' + '/'+AccountId;
    
    p.getParameters().put(Constants.SAVE_NEW_URL,save_Url);
    
    return p;
  }
  
  /************************************TEST************************************/
  public static testMethod void test()
  {
    Account acc = New Account();
    acc.Name='Test';
    insert acc;
 
    PageReference p  = Page.New_Opportunity_On_Account;
    p.getParameters().put('id',acc.id);
    Test.setCurrentPageReference(p);
    New_Opportunity_On_Account vf = new New_Opportunity_On_Account();
    vf.init();
  }

}

 

 

It seems to be due to the record type selection step.

I tried this and have no more error:

 

 public Pagereference init()
  {        
    string save_url ='/006/e?nooverride=1&opp4_lkid=001R000000iv3fc +'&' +
                    constants.CLOSED_DATE_FIELD_ID +'='
                        +constants.CLOSED_DATE +'&'+constants.RET_URL +'=/'+AccountId;  
    PageReference p = new PageReference(save_url);
return p;

 

 

But I don't know why ... and why the original code doesn't generate any error message.

Is there somebody who can help me understand?

 

(Please use easy english without any expression :) )

 

 

Ritesh AswaneyRitesh Aswaney

Hi,

Can you check what is the variable assigned to RECORD_TYPE_URL in the Contants class

Constants.RECORD_TYPE_URL