+ Start a Discussion
Nick KeehanNick Keehan 

Attempt to de-reference a null object - Wont Create Opportunity on Account

Hi Guys.

The below code will create an account fine, however it will not allow me to add an opportunity to the account with the error "System.NullPointerException: Attempt to de-reference a null object".

It is erroring at Opportunity.name (first line or opp)

Turned debug on however still cant see whats causing this. any ideas?

Apex Class
public class CreatingWizard{
Contact contact; 
Account account;
Opportunity opportunity;
OpportunityContactRole role;
public Contact getContact() {
if(contact == null) contact = new Contact();
return contact;}
public Account getAccount() {
if(account == null) account = new Account();
return account;}

public Opportunity getOpportunity() {
if(opportunity == null) opportunity = new Opportunity();
return opportunity;}
public OpportunityContactRole getRole() {
if(role == null) role = new OpportunityContactRole();
return role;}

   public PageReference createcustomer() {

               contact.LastName = Account.LastName;
               contact.FirstName = Contact.FirstName;
               insert contact;  

               account.LastName = Account.LastName;
               account.FirstName = Contact.FirstName;
               account.Phone = Account.Phone;
               account.PersonMobilePhone = Account.PersonMobilePhone;
               account.ShippingStreet = Account.ShippingStreet;                                             
               account.RecordTypeID = '012200000000iKt';
               account.Customer_Data_Source__c = 'https://c.cs86.visual.force.com/apex/accoppwizard';
               insert account;     

               opportunity.Name = 'Oneview Resign/Upgrade';
               opportunity.CampaignId = '7017E0000004srD';
               opportunity.CloseDate = system.today();
               opportunity.StageName = 'Closed Won';
               opportunity.accountId = '0017E00000IadeE';
               insert opportunity;  

                role.opportunityId = opportunity.id;
                role.contactId = contact.id;
                insert role;

               PageReference newPage = New PageReference('/'+opportunity.id);
               return newPage;

       return null;

Prateek Singh SengarPrateek Singh Sengar
Hi Nick,

I believe you will need to post the VF page to debug this issue. From you apex class i can see that the instantiation of the Account Contact and Opportunity is happening as part of a getter method. The method CreateCustomer I believe is executing on a click of a buton or link. 
Please ensure that the Opportunity is also instantiated via getter setter on your VF page before this button is clicked.

As an alternative you can instantiate your variables in constructor
// add this constructor to you class

public CreatingWizard()

account = new Account();
contact = new Contact();
opportunity = new Opportunity();
role = new OpportunityContactRole();

Nick KeehanNick Keehan
Thanks Prateek. Here is the VF page. will try above now and let you know how i go.
<apex:page controller="CreatingWizard" showHeader="false" sidebar="false" standardStylesheets="false" >
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">    
<div class="slds"> 

<div class="slds-box slds-theme--alt-inverse">
    <strong>Step 1</strong> - Capture the Customers Details</p>

<apex:stylesheet value="/resource/SLDS0102/assets/styles/salesforce-lightning-design-system.css"/>

<apex:form >

    <fieldset class="slds-box slds-theme--default slds-container--end">

      <legend id="newaccountform" class="slds-text-heading--large slds-p-vertical--large">Customer Account Details</legend>  

      <div class="slds-form-element">
        <label class="slds-form-element__label">First Name</label>
        <div class="slds-form-element__control">
          <apex:inputText value="{!Contact.FirstName}" id="FirstName" styleclass="slds-input"/>

     <div class="slds-form-element">
        <label class="slds-form-element__label">Last Name</label>
        <div class="slds-form-element__control">
          <apex:inputText value="{!Account.LastName}" id="LastName" styleclass="slds-input"/>
     <div class="slds-form-element">
        <label class="slds-form-element__label">Landline Number</label>
        <div class="slds-form-element__control">
          <apex:inputText value="{!Account.Phone}" id="Phone" styleclass="slds-input"/>

     <div class="slds-form-element">
        <label class="slds-form-element__label">Mobile Number</label>
        <div class="slds-form-element__control">
          <apex:inputText value="{!Account.PersonMobilePhone}" id="PersonMobilePhone" styleclass="slds-input"/>
     <div class="slds-form-element">
        <label class="slds-form-element__label">Street Address</label>
        <div class="slds-form-element__control">
          <apex:inputText value="{!Account.ShippingStreet}" id="ShippingStreet" styleclass="slds-input"/>
<apex:commandButton action="{!createcustomer}" value="Submit" styleClass="slds-button slds-button--brand slds-m-top--medium"/>