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
Pooja NekkalapudiPooja Nekkalapudi 

how to solve this ??Attempt to de-reference a null object

Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page crosssellvf: Class.CrossSellRExtensionController.save: line 17, column 1

public class CrossSellRExtensionController {
     public Account newPersonAccount {get; set;}
     public CrossSellRExtensionController(ApexPages.StandardController controller) {
    
           Cross_Sell_Opportunity__c csr = (Cross_Sell_Opportunity__c) controller.getRecord();
            
           csr = [Select Id, Name  from Cross_Sell_Opportunity__c where Id =: csr.Id];
           RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
           Account newPersonAccount = new Account();
           newPersonAccount.Cross_Sell_Referral__c = csr.Id;
   
          }
           
public PageReference save() {
       insert newPersonAccount; //this is line 17
        PageReference pageRef = new PageReference('/' + newPersonAccount.Cross_Sell_Referral__c);
        pageRef.setRedirect(true);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'new *contact* created.'));
        return pageRef;
     }
public PageReference cancel() {
     return new PageReference('/'+ newPersonAccount.Cross_Sell_Referral__c);
}
}
 
Best Answer chosen by Pooja Nekkalapudi
Amit Chaudhary 8Amit Chaudhary 8
Try below VF page
<apex:page standardcontroller="Cross_Sell_Opportunity__c"  extensions="CrossSellRExtensionController">
 <apex:sectionHeader title="Title" subtitle="Person Accounts"/>
    <apex:form >
		<apex:outputPanel >
		</apex:outputPanel>
      
    <apex:pageMessages />
    <apex:pageBlock title="Please fill in the details for creating *Contacts*">
        
		<apex:pageBlockSection >
			<apex:inputField value="{!Cross_Sell_Opportunity__c.Name}" required="true"/><br/>
			<apex:inputField value="{!newPersonAccount.Salutation}" /> &nbsp;
            <apex:inputField value="{!newPersonAccount.FirstName}"  /><br/>
            <apex:inputField value="{!newPersonAccount.LastName}"  required="true"/><br/>
            <apex:inputField value="{!newPersonAccount.PersonEmail}" required="true"/><br/>
            <apex:inputField value="{!newPersonAccount.Status__c}" /><br/>
        </apex:pageBlockSection>
		
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save" /> 
            <apex:commandButton action="{!cancel}" value="Cancel"/>
		</apex:pageBlockButtons>
        
	</apex:pageBlock>
    </apex:form>

</apex:page>
Below controller class
public class CrossSellRExtensionController {
    public Account newPersonAccount {get; set;}
    
	public CrossSellRExtensionController(ApexPages.StandardController controller) 
	{
           Cross_Sell_Opportunity__c csr = (Cross_Sell_Opportunity__c) controller.getRecord();
           csr = [Select Id, Name  from Cross_Sell_Opportunity__c where Id =: csr.Id];

           RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
           newPersonAccount = new Account();
           newPersonAccount.Cross_Sell_Referral__c = csr.Id;
		   newPersonAccount.recordtypeId = personAccountRecordType.id ;
    }
           
	public PageReference save() 
	{
			insert newPersonAccount; //this is line 17
			PageReference pageRef = new PageReference('/' + newPersonAccount.Cross_Sell_Referral__c);
			pageRef.setRedirect(true);
			ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'new *contact* created.'));
			return pageRef;
	}
	public PageReference cancel() 
	{
		 return new PageReference('/'+ newPersonAccount.Cross_Sell_Referral__c);
	}

}
Let us know if this will help you


 

All Answers

venkat-Dvenkat-D
Replace  Account newPersonAccount = new Account(); in the controller with 
newPersonAccount = new Account();

You are createing new variable newPersonAccount  with Account before that effectively making newPersonaccount in class and controller as different variables
Pooja NekkalapudiPooja Nekkalapudi
Hi , Thanks but this popped a new error 
Insert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Name: [Name]
Error is in expression '{!save}' in component <apex:commandButton> in page crosssellvf: Class.CrossSellRExtensionController.save: line 16, column 1
 
Richard Jimenez 9Richard Jimenez 9
Hi Pooja,

You will need to specify the firstname and lastname values on your person account before you insert it.

See this link for more details: http://help.salesforce.com/apex/HTViewSolution?id=000181428&language=en_US

Thanks,
Richard.
venkat-Dvenkat-D
Yes..LastName is requried. so populate last name and try
Pooja NekkalapudiPooja Nekkalapudi
This my VF page

<apex:page standardcontroller="Cross_Sell_Opportunity__c"  extensions="CrossSellRExtensionController">
 <apex:sectionHeader title="Title" subtitle="Person Accounts"/>
    <apex:form >
      <apex:outputPanel >
  
  </apex:outputPanel>
      
        <apex:pageMessages />
        <apex:pageBlock title="Please fill in the details for creating *Contacts*">
        
      <apex:pageBlockSection >
        
        <apex:inputField value="{!Cross_Sell_Opportunity__c.Name}" required="true"/><br/>
        <apex:inputField value="{!newPersonAccount.Salutation}" /> &nbsp;
            <apex:inputField value="{!newPersonAccount.FirstName}"  /><br/>
            <apex:inputField value="{!newPersonAccount.LastName}"  required="true"/><br/>
             <apex:inputField value="{!newPersonAccount.Name}" required="true"/><br/>
             <apex:inputField value="{!newPersonAccount.PersonEmail}" required="true"/><br/>
              <apex:inputField value="{!newPersonAccount.Status__c}" /><br/>
     
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save" /> 
         
            <apex:commandButton action="{!cancel}" value="Cancel"/>

         
</apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>

</apex:page>

Even though I set input fields last name and first name it shows blank no way to edit 
User-added image
 
venkat-Dvenkat-D
You are querying for the record type but not assigning that to the record.
newPersonAccount.RecordTypeId = personAccountRecordType.Id;

Add this in controller and try
Pooja NekkalapudiPooja Nekkalapudi
tired boht these stmts:

 newPersonAccount.RecordTypeId = personAccountRecordType.Id;
       newPersonAccount.RecordTypeId='012E0000000QtIm';
Amit Chaudhary 8Amit Chaudhary 8
Try below VF page
<apex:page standardcontroller="Cross_Sell_Opportunity__c"  extensions="CrossSellRExtensionController">
 <apex:sectionHeader title="Title" subtitle="Person Accounts"/>
    <apex:form >
		<apex:outputPanel >
		</apex:outputPanel>
      
    <apex:pageMessages />
    <apex:pageBlock title="Please fill in the details for creating *Contacts*">
        
		<apex:pageBlockSection >
			<apex:inputField value="{!Cross_Sell_Opportunity__c.Name}" required="true"/><br/>
			<apex:inputField value="{!newPersonAccount.Salutation}" /> &nbsp;
            <apex:inputField value="{!newPersonAccount.FirstName}"  /><br/>
            <apex:inputField value="{!newPersonAccount.LastName}"  required="true"/><br/>
            <apex:inputField value="{!newPersonAccount.PersonEmail}" required="true"/><br/>
            <apex:inputField value="{!newPersonAccount.Status__c}" /><br/>
        </apex:pageBlockSection>
		
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save" /> 
            <apex:commandButton action="{!cancel}" value="Cancel"/>
		</apex:pageBlockButtons>
        
	</apex:pageBlock>
    </apex:form>

</apex:page>
Below controller class
public class CrossSellRExtensionController {
    public Account newPersonAccount {get; set;}
    
	public CrossSellRExtensionController(ApexPages.StandardController controller) 
	{
           Cross_Sell_Opportunity__c csr = (Cross_Sell_Opportunity__c) controller.getRecord();
           csr = [Select Id, Name  from Cross_Sell_Opportunity__c where Id =: csr.Id];

           RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
           newPersonAccount = new Account();
           newPersonAccount.Cross_Sell_Referral__c = csr.Id;
		   newPersonAccount.recordtypeId = personAccountRecordType.id ;
    }
           
	public PageReference save() 
	{
			insert newPersonAccount; //this is line 17
			PageReference pageRef = new PageReference('/' + newPersonAccount.Cross_Sell_Referral__c);
			pageRef.setRedirect(true);
			ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'new *contact* created.'));
			return pageRef;
	}
	public PageReference cancel() 
	{
		 return new PageReference('/'+ newPersonAccount.Cross_Sell_Referral__c);
	}

}
Let us know if this will help you


 
This was selected as the best answer
Richard Jimenez 9Richard Jimenez 9
Pooja,

Have you looked into using Visual Flow to do this? - it feels like it would be a good use case. You will also find it easier for admins to use and configure. You can start your flow from a custom button or embed in a VF page.

https://www.youtube.com/watch?v=nkdc_133qHk
https://www.youtube.com/watch?v=R8fNHGWxW2I

Thanks,
Richard.