I have the below vf Page through which I am trying to enter some data and saving it to custom object.

visualforce page:

<apex:page Controller="SubContractorController2"  docType="html-5.0">
<apex:form >
<apex:sectionHeader title="Subcontractor Company Edit" subtitle="New 
Subcontractor Company">
    <apex:pageBlock title="Subcontractor Company Edit">
    <apex:pageblockButtons >
            <apex:commandButton value="Save" action="{!Save}"/>

     <apex:pageBlockSection title="Information" columns="2">

            <apex:inputfield value="{!Subcompany.Name}" />
            <apex:inputfield value="{!Subcompany.Address__c}"/>
            <apex:inputField value="{!Subcompany.Website__c}"/>
            <apex:inputField value="{!Subcompany.City__c}"/>
            <apex:inputField value="{!Subcompany.Pre_Qualified__c}"/>
            <apex:inputField value="{!Subcompany.State__c}"/>
            <apex:inputField value="{!Subcompany.License_Number__c}"/>
            <apex:inputField value="{!Subcompany.Zip__c}"/>
            <apex:inputField value="{!Subcompany.Main_Phone_Number__c}" />
            <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Subcompany.Trade__c}"/>

apex controller:

public class SubContractorController2 {
   Public Subcontractor_Company__c    Subcompany {get;set;}
   Public Subcontractor_Contact__c    Subcontact{get;set;}

   public void Save() {
        Subcontractor_Company__c Subcompany2 = NeW             

        Subcompany2.Name = Subcompany.Name; 
        Subcompany2.Address__c = Subcompany.Address__c;
        Subcompany2.Website__c = Subcompany.Website__c;
        Subcompany2.City__c = Subcompany.City__c;
        Subcompany2.Pre_Qualified__c  = Subcompany.Pre_Qualified__c;
        Subcompany2.State__c = Subcompany.State__c;
        Subcompany2.License_Number__c = Subcompany.License_Number__c;
        Subcompany2.Zip__c = Subcompany.Zip__c;
        Subcompany2.Main_Phone_Number__c = Subcompany.Main_Phone_Number__c;
        Subcompany2.Trade__c = Subcompany.Trade__c;

        Insert Subcompany2;

When I try to save I am getting the error-message "Attempt to de-reference a NULL Object".

Getting error message as below:

System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!Save}' in component in page subcontractcompanyvfpage: Class.SubContractorController2.Save: line 11, column 1 Class.SubContractorController2.Save: line 11, column 1

  • 1
    Please add the complete error message including the line that causes the error. Furthermore, as line numbers aren't displayed on stackexchange, please add the code on this line as a separate part of your question. Thank you! – Markus Slabina May 22 '17 at 15:01
  • Are you trying to achieve a custom clone? If so, you should be writing an extension, not a controller. – Adrian Larson May 22 '17 at 15:16

You need to actually instantiate subCompany. Add a constructor to your controller:

public with sharing class MyController
    // properties

    public MyController()
        subCompany = new MyObject__c();

    // methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.