+ Start a Discussion
chanti kchanti k 

How to resolve the System.NullPointerException: Attempt to de-reference a null object

Hello all,
While saving a record now i am getting beloww error on command button , kindly do the needful.

Here i have attached the my code. pls see attached screen shot User-added image


Thanks,
Chanti










 
Apex class :Public class employmenthistiry
{

 public Employee__c emp{get;set;}
 public list<Employee__c > empl{get;set;}
 public List<SelectOption> Technicaloptions{get;set;}
 
 public employmenthistiry()
 {
 
 empl=[select id,name,Date_of_joining__c,Department__c,Desigination__c,Email__c,Employeeid__c,Mobile__c,Office_Phone__c,payslip_lase_sent_date__c,Permanent_cITY__c from Employee__c];
 
  
 }
 
 
 public PageReference submit()
 {
 insert emp;
 return null;
 }
 
}



PAGE

<apex:page controller="employmenthistiry" sidebar="true">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Employe" collapsible="false">
                    <apex:inputField value="{! emp.Name }"/>
                    <apex:inputField value="{! emp.Date_of_joining__c}"/>
                    <apex:inputField value="{! emp.Department__c}"/>
                    <apex:inputField value="{! emp.Desigination__c}"/>
                    <apex:inputField value="{! emp.Email__c}"/>
                    <apex:inputField value="{! emp.Employeeid__c}"/>
                    <apex:inputField value="{! emp.Mobile__c }"/>
                    <apex:inputField value="{! emp.Office_Phone__c}"/>
                    <apex:inputField value="{! emp.payslip_lase_sent_date__c}"/>
                    <apex:inputField value="{! emp.Technology__c}"/>
                    </apex:pageBlockSection>
                    
             <apex:pageBlockSection TItle="ADDRESS">
             
                    <apex:inputField value="{! emp.Permanent_cITY__c }"/>
                    <apex:inputField value="{! emp.Permanent_State__c}"/>
                    <apex:inputField value="{! emp.PIN_CODE__c}"/>
                    <apex:inputField value="{! emp.Permanent__c}"/>
                    <apex:inputField value="{! emp.Name }"/>
                    <apex:inputField value="{! emp.Village__c}"/>
            </apex:pageBlockSection>   
            
            <apex:commandButton value="submit" action="{!submit}" />  
                
              
        </apex:pageBlock>
  </apex:form>
</apex:page>

 
Varun SinghVarun Singh

hi@
chanti k
you are getting error  on this  line
emp is single record you have  create soql  with specific id  or limit 1
or you have to pass id from
 
public employmenthistiry()
 {
 
 empl=[select id,name,Date_of_joining__c,Department__c,Desigination__c,Email__c,Employeeid__c,Mobile__c,Office_Phone__c,payslip_lase_sent_date__c,Permanent_cITY__c from Employee__c];
 
  
 }
[select id,name,Date_of_joining__c,Department__c,Desigination__c,Email__c,Employeeid__c,Mobile__c,Office_Phone__c,payslip_lase_sent_date__c,Permanent_cITY__c from Employee__c limit1]

or 
[select id,name,Date_of_joining__c,Department__c,Desigination__c,Email__c,Employeeid__c,Mobile__c,Office_Phone__c,payslip_lase_sent_date__c,Permanent_cITY__c from Employee__c where id ='rec id'];

on page url you have to put id of a record like you page name is CloseLeadReason
https://c.cs8.visual.force.com/apex/CloseLeadReason?id='00635234244xcd'
​00635234244xcd is record if of Employee__c

 
Varun SinghVarun Singh
to fetch record try in this way

Apex controller:
 
global with sharing class poistionController {

     public Position__c pos{get;set;}

     public poistionController() {

         pos = [select Max_Pay__c from Position__c where Name = 'Sr. Java Developer'];

      }

}

vf page
 
<apex:page controller="poistionController" showHeader="false">
   <div align="center" width="550px">
      <h1>Congratulations</h1>
      This is your new Page : <b>Hello World ...!</b><br />
      <b>
         <apex:outputText value="Your maximum salary could be AT MAXIMUM {!pos.Max_Pay__c}"/>
      </b>
   </div>
</apex:page>

Asif Ali MAsif Ali M
The issue is with the below line.
public Employee__c emp{get;set;}
The default value for any APEX variable is null. To avoid this error always initialize your variable with correct Types. dont just define them and leave the null as default value. To fix your issue either update the get or update the constructor. 

Option 1: 
public Employee__c emp{get{ if(emp == null) return new Employee__c();  };set;}


Option 2:
 
public employmenthistiry() {
    emp = new Employee__c();
    empl = [select id, name, Date_of_joining__c, Department__c, Desigination__c, Email__c, Employeeid__c, Mobile__c, Office_Phone__c, payslip_lase_sent_date__c, Permanent_cITY__c from Employee__c];

}


Also note that your SOQL query in above code will hit system limits when there are more that 50k records. 


 
Amit Chaudhary 8Amit Chaudhary 8
Hi,

Issue is coming because you did not created the object of emp object in your Apex class and you are querying empl object not emp.

NOTE:-// No need of below code as you are not using same in VF page
empl=[select id,name,Date_of_joining__c,Department__c,Desigination__c,Email__c,Employeeid__c,Mobile__c,Office_Phone__c,payslip_lase_sent_date__c,Permanent_cITY__c from Employee__c];

Try to update your code like below
Public class employmenthistiry
{

	public Employee__c emp{get;set;}
	public list<Employee__c > empl{get;set;}
	public List<SelectOption> Technicaloptions{get;set;}

	public employmenthistiry()
	{
	
		emp = new Employee__c();
		// No need of below code as you are not using same in VF page
		empl=[select id,name,Date_of_joining__c,Department__c,Desigination__c,Email__c,Employeeid__c,Mobile__c,Office_Phone__c,payslip_lase_sent_date__c,Permanent_cITY__c from Employee__c];
	}


	public PageReference submit()
	{
		insert emp;
		return null;
        // return new PageReference('/'+emp.id); // you can try this line as well
	}
 
}

Let us know if this will help you
 
Glyn Anderson 3Glyn Anderson 3
If you are just trying to save the user's edits to the Employee record, you don't need a controller at all.  Just use the standard controller, which includes a "save" action:

<pre>
<apex:page standardController="Employee__c" sidebar="true">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Employee" collapsible="false">
                    <apex:inputField value="{!Employee__c.Name }"/>
                    <apex:inputField value="{!Employee__c.Date_of_joining__c}"/>
                    <apex:inputField value="{!Employee__c.Department__c}"/>
                    <apex:inputField value="{!Employee__c.Desigination__c}"/>
                    <apex:inputField value="{!Employee__c.Email__c}"/>
                    <apex:inputField value="{!Employee__c.Employeeid__c}"/>
                    <apex:inputField value="{!Employee__c.Mobile__c }"/>
                    <apex:inputField value="{!Employee__c.Office_Phone__c}"/>
                    <apex:inputField value="{!Employee__c.payslip_lase_sent_date__c}"/>
                    <apex:inputField value="{!Employee__c.Technology__c}"/>
             </apex:pageBlockSection>

             <apex:pageBlockSection TItle="Address">
                    <apex:inputField value="{!Employee__c.Permanent_City__c }"/>
                    <apex:inputField value="{!Employee__c.Permanent_State__c}"/>
                    <apex:inputField value="{!Employee__c.PIN_CODE__c}"/>
                    <apex:inputField value="{!Employee__c.Permanent__c}"/>
                    <apex:inputField value="{!Employee__c.Name }"/>
                    <apex:inputField value="{!Employee__c.Village__c}"/>
            </apex:pageBlockSection>

            <apex:commandButton value="Submit" action="{!save}" />
        </apex:pageBlock>
  </apex:form>
</apex:page>
</pre>
 
Glyn Anderson 3Glyn Anderson 3
chanti k, Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved it yourself another way, please post your solution.  Thanks!