+ Start a Discussion
nagu90007nagu90007 

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

Visualforce page:

------------------------

<apex:page standardController="Proposal__c" extensions="npList">
<apex:sectionHeader subtitle="New Proposal" title="Proposal Edit" />
<apex:form >
<apex:pageBlock title="Proposal Edit">

<apex:pageBlockButtons location="top">
<apex:commandButton action="{!gettingRecords}" value="Records List" immediate="true"/>
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!saveandnew}" value="Save & New"/>
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
</apex:pageBlockButtons>

<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!gettingRecords}" value="Records List" immediate="true"/>
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!saveandnew}" value="Save & New"/>
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Information">
<apex:inputField value="{!acc.Name}" required="true"/>
<apex:inputField value="{!acc.Opportunity__c}" required="true"/>
<apex:inputField value="{!acc.Proposal_Name__c}"/>
<apex:inputField value="{!acc.Initial_Deal_Review_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Type__c}"/>
<apex:inputField value="{!acc.Mid_Poin_Deal_Review_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Recieved__c}"/>
<apex:inputField value="{!acc.Final_Deal_Review_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Due_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Submit_Date__c}"/>
<apex:inputField value="{!acc.Down_Select_Target_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Final_Selection_Date__c}"/>
<apex:inputField value="{!acc.RFP_Selected__c}"/>
<apex:inputField value="{!acc.Link_to_Proposal__c}"/>
<apex:inputField value="{!acc.Status__c}"/>
<apex:inputField value="{!acc.Link_to_Pricing__c}"/>
<apex:inputField value="{!acc.Down_Selected__c}"/>
<apex:inputField value="{!acc.Down_Selected_Date__c}"/>
</apex:pageBlockSection>

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

</apex:page>

 

 

 

 

 

 

controller:

--------------

 

public with sharing class npList {
private ApexPages.StandardController controller {get; set;}
public npList(ApexPages.StandardController controller) {
// controller.npList =null;
}
public Proposal__c acc {get;set;}
public String getProposal() {
return null;
}
public npList()
{
acc = new Proposal__c();
}
public PageReference gettingRecords() {
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/a07/o');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference save() {
insert acc;
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/006/o');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference saveandnew() {
insert acc;
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference cancel() {
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
return null;
}}

 

 

 

Hi ,

Here I created one visualforce page and save and save&new button.Here my problem is,if  i click on save and save & new button then i will get an error like "System.NullPointerException: Attempt to de-reference a null object',"Error is in expression '{!save}' in page np" . Here the records are not stored into particular object ,so let me know what is the solution for this error and how to save the values.

 

Thanks in advance....

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

write the below line

 

acc = new Proposal__c();

 

to constructor.

public npList(ApexPages.StandardController controller) {
     // controller.npList =null;
    acc = new Proposal__c();
}

 because you have used Standard Controller in your visualforce page.

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

souvik9086souvik9086

The error is when you make pagereference in "np" page.

PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');

 

Just check in the "np" page whether it will need any argument to pass. I think there is some argument needed to be passed and as there is nothing going so it is showing null pointer exception. If there is some paramater needed then please send that parameter
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np?id='+//Your id);

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

hitesh90hitesh90

Hi,

 

write the below line

 

acc = new Proposal__c();

 

to constructor.

public npList(ApexPages.StandardController controller) {
     // controller.npList =null;
    acc = new Proposal__c();
}

 because you have used Standard Controller in your visualforce page.

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
nagu90007nagu90007

Hi souvik,

 

              Thanks for giving the reply but, here the page reference is working properly but the main problem is values  not inserted into the 'Proposal' object .In my controller the line "insert acc;" not working.Let me know how it is.

 

Thank you.

Avidev9Avidev9

I am with hitesh and seems like acc was not initialised.

try this out...

 

public with sharing class npList {
      public npList(ApexPages.StandardController controller) {
        acc = new Proposal__c();
    }
    public Proposal__c acc {
        get;
        set;
    }
    public String getProposal() {
        return null;
    }
    
    public PageReference gettingRecords() {
        PageReference submissionPage = new PageReference('https://ap1.salesforce.com/a07/o');
        submissionPage.setRedirect(true);
        return submissionPage;
    }
    public PageReference save() {
        insert acc;
        PageReference submissionPage = new PageReference('https://ap1.salesforce.com/006/o');
        submissionPage.setRedirect(true);
        return submissionPage;
    }
    public PageReference saveandnew() {
        insert acc;
        PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
        submissionPage.setRedirect(true);
        return submissionPage;
    }
    public PageReference cancel() {
        PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
        return null;
    }
}

 

souvik9086souvik9086

Initialization problem as pointed out above by Hitesh. You are using standard controller and you didn't initialize new in the standard controller constructor. You just did it in default constructor. Please find the below blue colored code.

 

public with sharing class npList {
private ApexPages.StandardController controller {get; set;}
public npList(ApexPages.StandardController controller) {
acc = new Proposal__c();
}
public Proposal__c acc {get;set;}
public String getProposal() {
return null;
}
public npList()
{
acc = new Proposal__c();
}
public PageReference gettingRecords() {
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/a07/o');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference save() {
insert acc;
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/006/o');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference saveandnew() {
insert acc;
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference cancel() {
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
return null;
}}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

nagu90007nagu90007

Hi Hitesh,

        You gave the perfect solution  to my application ,thank you very much.

 

 

 

Sfd developerSfd developer

Hi,

 

Try this,

 

public with sharing class npList {
private ApexPages.StandardController controller {get; set;}
public npList(ApexPages.StandardController con) {
controller = con;
}

public PageReference gettingRecords() {
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/a07/o');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference save() {
Proposal__c acc = (Proposal__c)controller.getRecord();
insert acc;
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/006/o');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference saveandnew() {
Proposal__c acc = (Proposal__c)controller.getRecord();
insert acc;
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
submissionPage.setRedirect(true);
return submissionPage;
}
public PageReference cancel() {
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/np');
return null;
}}

nagu90007nagu90007

Hi souvik,

 

Thank you ,its working.

nagu90007nagu90007

Hi,

 

 

 Thanks to all..Its working.

nagu90007nagu90007

 Hi,

 

 

I have one more problem,that is,in my visualforfce page I have code like "<apex:inputField value="{!Proposal__c.Opportunity__c}"/>" this is look up field, when ever i click on 'new' button under proposal tab at the time the look up field will be in editable mode ,and next  clicking on new button from oppartunity tab (it had a relationship with proposal tab) then that time it will be non editable and it will come with default value,is it possible ,if it is possible please let me know how it is?

 

Thanks in advance..

 

souvik9086souvik9086

Please find the below code

 

<apex:page standardController="Proposal__c" extensions="obList">
<apex:sectionHeader subtitle="New Proposal" title="Proposal Edit"/>
<apex:form >
<apex:pageBlock title="Proposal Edit">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!saveandnew}" value="Save & New"/>
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
<apex:commandButton action="{!recordslist}" value="RecordsList" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!saveandnew}" value="Save & New"/>
<apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
<apex:commandButton action="{!recordslist}" value="RecordsList" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Information">
<apex:inputField value="{!acc.Name}" required="true"/>
<apex:inputField value="{!acc.Opportunity__c}" rendered="{!editable == true}"/>
<apex:outputField value="{!acc.Opportunity__c}" rendered="{!editable == false}"/>
<apex:inputField value="{!acc.Type__c}"/>
<apex:inputField value="{!acc.Initial_Deal_Review_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Received__c}"/>
<apex:inputField value="{!acc.Midpoint_Deal_Review_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Due_Date__c}"/>
<apex:inputField value="{!acc.Final_Deal_Review_Date__c}"/>
<apex:inputField value="{!acc.Down_Select_Target_Date__c}"/>
<apex:inputField value="{!acc.Proposal_Submit_Date__c}"/>
<apex:inputField value="{!acc.RFP_Submitted__c}"/>
<apex:inputField value="{!acc.Proposal_Final_Selection_Date__c}"/>
<apex:inputField value="{!acc.Status__c}"/>
<apex:inputField value="{!acc.Link_to_Proposal__c}"/>
<apex:inputField value="{!acc.Down_Selected__c}"/>
<apex:inputField value="{!acc.Link_to_Pricing__c}"/>
<apex:inputField value="{!acc.Down_Selected_Date__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


This is my controller:
--------------------------------
public with sharing class obList {
private ApexPages.StandardController controller {get; set;}
public Boolean editable{get;set;}
public obList(ApexPages.StandardController controller) {
acc = new Proposal__c();
this.acc = (PF_Lead__c)controller.getRecord();
if(acc.Opportunity__c != NULL && acc.Opportunity__c != ''){
editable = false;
}
else{
editable = true;
}

} public Proposal__c acc {get;set;}
public String getProposal() {
return null;
}
public obList()
{
// instantiate your dummy object
acc = new Proposal__c();
}

public PageReference recordslist() {
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/a0C/o');
return submissionPage;
}

public PageReference cancel() {
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/proposal');
return null;
}


public PageReference saveandnew() {
insert acc;
PageReference submissionPage = new PageReference('https://c.ap1.visual.force.com/apex/proposal');
submissionPage.setRedirect(true);
return submissionPage;
}

 

public PageReference save() {
insert acc;
PageReference submissionPage = new PageReference('https://ap1.salesforce.com/a0B/o');
submissionPage.setRedirect(true);
return submissionPage;
}

}

 

If the post helps you please throw KUDOS

nagu90007nagu90007

Hi souvik,

 

Thank you very much,Its working...

 

Thanks..