0

Getting 'Attempt to de-reference a null object' on the visualforce page. The objective is to open the correct page when viewing the record based on a corresponding record type. The record type 'New_Account_Summary' should take the user to a corresponding visualforce page. Any other reocrd type should open in Force.com page.

Visualforce Page

<apex:page standardStylesheets="true" standardController="Account_Summary__c" readOnly="false" extensions="AccountSummaryController" showHeader="true" action="{!redirectCREPage}"  > 

Controller

public string RecordTypeName { get; set; }
public string recordName { get; set; }  

    //Constructors
public AccountSummaryController(ApexPages.StandardController controller) {
    try {
        this.stdController = controller;
        this.AccountSummary = (Account_Summary__c) controller.getRecord();
        this.ParentAcct = new Account();
        this.IsNew = (this.AccountSummary.Id == null);
        this.hidecols = true; //toggles hidden columns. true=show all hidden columns.
        risk1 = 'null';

        if (this.AccountSummary.Report_Status__c == 'Final') {
            this.statusupdate = true; //toggles report status. true=report status is final.
        } else {
            this.statusupdate = false;
        }

        if (!this.IsNew) {
            //Lock record processing
            this.isLocked = Approval.isLocked(this.AccountSummary.Id);  //test is record is locked via approval request.   //causing error Arg 1 cannot be null.
            if (this.isLocked) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning, 'CRE locked. Report Status set to Final. To edit click button "Mark CRE as Draft."'));
            }
        }

        //Get Url Parameters
        if (this.IsNew) {
            Map<string, string> params = ApexPages.currentPage().getParameters();
            //this.RecordTypeId = params.get(URL_PARAM_RECORD_TYPE_ID);   //only needed for new record
            this.AccountId = params.get(URL_PARAM_ACCOUNT_ID);          //only needed for new record
            this.ExternalId = params.get(URL_PARAM_EXTERNAL_ID);        //only needed for new record
            this.SubmissionYear = params.get(URL_PARAM_SUBMISSION_YEAR);
            this.Name = params.get(URL_PARAM_SUBMISSION_NAME);
            //this.ExternalId = ParentAcct.External_ID_BA__c;
        }

        //Get Submission Year   //used to query for financial submission and contract records.
        this.SubmissionYear = this.AccountSummary.Name;


        if (SubmissionYear == null) {
            SubmissionYear = this.SubmissionYear;  //grabs value passed in thru URL as new record.
        }
        if (SubmissionYear == null) {  // need a year if the url year is null as well.
            SubmissionYear = string.valueof(system.today().year());
        }
        if (SubmissionYear != null) {
            this.accountsummary.name = this.SubmissionYear;
            this.ContractYear = string.valueof((integer.valueof(SubmissionYear) - 1));
        } else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Invalid Submission Year. Please contact the help desk with this error'));
        }


        //Get Account Id Prefix
        Schema.DescribeSObjectResult dsr = Account.sObjectType.getDescribe();  // errored when
        this.accountIdPrefix = dsr.getKeyPrefix();

        //Get Account Id
        Id AccountId = this.AccountSummary.Account_Name__c;

        //Get RecordType  //used to redirect to correct pagetype when loaded.   
        //this.RecordTypeId =  ApexPages.currentPage().getParameters().get('RecordType');
        //this.RecordTypeName = this.AccountSummary.RecordTypeName__c;
String RecordTypeName = Account_Summary__c.SObjectType.getDescribe().getRecordTypeInfosById().get(AccountSummary.RecordTypeId).getName();             
System.Debug('TEST1 RECORDTYPENAME is : ' + this.RecordTypeName );
String recordName = Account_Summary__c.SObjectType.getDescribe().getRecordTypeInfosById().get(AccountSummary.RecordTypeId).getName();
System.Debug('TEST2 RECORDTYPENAME is : ' + recordName );            
        if (AccountId == null) {
            //string param1 = ApexPages.currentPage().getParameters().get('AccId');
            string param1 = this.AccountId;  //grabs value passed in thru URL as new record.

            if (isAccountId(param1)) {
                AccountId = param1;
            } else {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Invalid Account Id. Please contact the help desk with this error'));
            }
        }

        //Get External Account Business Affiliate Id   //used to query acct for submission records.
        ExternalId = this.AccountSummary.AccountExt_ID_BA__c;

        if (ExternalId == null) {
            ExternalId = this.ExternalId;  //grabs value passed in thru URL as new record.
            ExternalId = ApexPages.currentPage().getParameters().get('exid');
        }


        //Load Account Record
        List<Account> accounts = queryAccounts(AccountId);

        if (accounts.isEmpty() == true) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'No records were found. Please contact the help desk with this error'));
        } else if (accounts.size() > 1) {

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'More than 1 record found. Please contact the help desk with this error'));

        }

        else {
            this.ParentAcct = accounts[0];
            this.AccountSummary.Account_Name__c = this.ParentAcct.Id;
        }

        SelectedSubmissions = new List<String>();

        if (!this.IsNew) {
            //Get stored selection options when re-opening saved record.
            this.SubmissionOptions = getSubmissionOptions(this.ExternalId, this.SubmissionYear);    //this is adding 2 bogus submission options
            this.SelectedSubmissions = toArray(this.AccountSummary.SelectedOptions__c);
            getSelectedSubmissions();   //used to fetch all submission options
        }

        else {
            this.SubmissionOptions = getSubmissionOptions(this.ExternalId, this.SubmissionYear);    //this is adding 2 bogus submission options
        }


    } catch (Exception ex) {
        ApexPages.addMessages(ex);
    }

}


    //Public Methods
    //Page redirection

     public PageReference redirectCREPage() {    

        PageReference pageRef = null;    
        //Get RecordType  //used to redirect to correct pagetype when loaded.   
        this.RecordTypeName = this.AccountSummary.RecordTypeName__c;
         if (!this.IsNew) {   // only process for saved records       
            try {
                 System.Debug('TEST RECORDTYPENAME is : ' + this.RecordTypeName ); 
             if(this.RecordTypeName.equals('New_Account_Summary'))
                {   
                    pageRef = new PageReference('/apex/PROD_UW_AccountSummary');
                    pageRef.getParameters().put('id', this.AccountSummary.Id);
                    pageRef.getParameters().put('nooverride', '1');
                    pageRef.setRedirect(true);  
                }
                else 
                {
                    pageRef = new PageReference('/' + this.AccountSummary.Id);
                    pageRef.getParameters().put('inline', '0');
                    pageRef.getParameters().put('nooverride', '1');
                    pageRef.setRedirect(true);   
                }

                } catch (Exception ex) {
                    ApexPages.addMessages(ex);
                }
                   return pageRef;

                } // end if a New record
            return pageRef;
            }     
6
  • on which line you are getting this error? Jul 17, 2017 at 22:42
  • Not getting any error on the controller. Seeing the error on the VisualForce page when I add the action to the page.
    – rickmac
    Jul 17, 2017 at 22:43
  • you need to put a debug statement, I think this.RecordTypeName could be null Jul 17, 2017 at 22:47
  • I verified this is indeed the case . Add the System.Debug to the code above and it yields null for this.RecordTypeName. Not sure why. In fact this is null if I attempt to get this.RecordTypeName up in the contoller. This is a formula field as RecordType.DeveloperName
    – rickmac
    Jul 17, 2017 at 23:14
  • I don't think you can access recordtype name from the standard controller. What happens if you instead cast a string of the recordtype name from the standard controllers recordtypeid? Jul 18, 2017 at 0:06

1 Answer 1

3

It will be better the retrieve recordtype name based on recordtypeId like this:

String rtName = Account.SObjectType.getDescribe().getRecordTypeInfosById().get(AccountSummary.RecordTypeId).getName();
1
  • Weird: I get null when using RecordTypeName as a variable. I get the record name if I use a variable name of something different like recordName. Is RecordTypeName a reserved word ? See edited lines above.
    – rickmac
    Jul 18, 2017 at 13:53

You must log in to answer this question.

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