+ Start a Discussion
RajiiiRajiii 

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

Hi,

   I created Page for custom save controller. my page apex code given below...

 

<apex:page controller="newOpportunityController11" tabStyle="Account">
<apex:form >
<apex:pageBlock title="Congratulations">
<apex:inputField value="{!Invoice.Name}"/>
<apex:inputField value="{!Invoice.Line_Item__c}"/>
<apex:inputField value="{!Invoice.Pay_by_Date__c}"/>
<apex:inputField value="{!Invoice.Total_Amount__c}"/>
<apex:inputField value="{!Invoice.Client_Name__c}"/>
<apex:inputField value="{!Invoice.Invoice_Status__c}"/>
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlock>
</apex:form>
</apex:page>
and my apex class is...
public class newOpportunityController11
 {  
Opportunity opportunity;
public Invoice__c invoice{ get; set; }
public Opportunity getOpportunity() {
//if(opportunity == null) opportunity = new Opportunity();
return opportunity;
}
public Invoice__c getinvoice() {
//if(Invoice == null) Invoice = new Invoice__c();
Id id = ApexPages.currentPage().getParameters().get('id');
invoice= [SELECT Client_Name__c,Invoice_Status__c,Line_Item__c,Pay_by_Date__c,Total_Amount__c FROM Invoice__c WHERE id = :id];
return invoice;
}
public PageReference cancel() {
PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();
opportunityPage.setRedirect(true);
return opportunityPage;
}
public PageReference save() 
{
update invoice;
return null;
}
}
the above page and class was successfully save without error... and then when i run this page and enter the data then click save button the error given below error is occur...
System.NullPointerException: Attempt to de-reference a null object

 

Class.newOpportunityController11.save: line 47, column 8 External entry point
How to solve this....pls help

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dotnet developedotnet develope

Hi Friend,

 

I can see the code well written, but to run this code sucessfully you need to some chages on it.

 

first

    define standardcontroller attribute on page tag ie "standandcontroller=Invoice__c"

Second

    define extension attribute on page tag ie "extension=newOpportunityController11"

 

thats it, it will work........

 

Thanks & Regards,

 

All Answers

cogcog

What is line 47 in your class? I don't see it here. Make sure that variable/object you are using in that line is not null (check before you call any method over that variable/object). 

Pradeep_NavatarPradeep_Navatar

This error comes when you are getting null value.Check by using system.debug in controller and track your code.

 

orikkerorikker

Make sure you pass record Ids to the page. 

cloud-developmentcloud-development

It may be because you are returning a null PageReference on your save method, surely you need to return a reference to the opportunity?

r_boyd_848r_boyd_848

Add a class constructor

 

public newOpportunityController11(){

         Id id = ApexPages.currentPage().getParameters().get('id');
         invoice= [SELECT Client_Name__c,Invoice_Status__c,Line_Item__c,Pay_by_Date__c,Total_Amount__c FROM             Invoice__c WHERE id = :id];

}

 

Assuming the Id is being passed through the URL .

I can't see where getInvoice is being called to intialise the Invoice variable so use the Class constructor. Also you have public Invoice__c invoice{ get; set; } and getInvoice, you can't have both. It should be either public Invoice__c invoice; then getInvoice will execute.

 

 

crazycloudcrazycloud

hi,

 

try this.

ublic class newOpportunityController11

Opportunity opportunity;
public Invoice__c invoice{
get
{
//if(Invoice == null) Invoice = new Invoice__c();
String strId = ApexPages.currentPage().getParameters().get('id');
invoice= [SELECT Client_Name__c,Invoice_Status__c,Line_Item__c,Pay_by_Date__c,Total_Amount__c FROM Invoice__c WHERE id = :strId];
return invoice;
}
set; }
public Opportunity getOpportunity() {
//if(opportunity == null) opportunity = new Opportunity();
return opportunity;
}
/**
public Invoice__c getinvoice() {
//if(Invoice == null) Invoice = new Invoice__c();
Id id = ApexPages.currentPage().getParameters().get('id');
invoice= [SELECT Client_Name__c,Invoice_Status__c,Line_Item__c,Pay_?by_Date__c,Total_Amount__c FROM Invoice__c WHERE id = :id];
return invoice;
}
*/
public PageReference cancel() {
PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();
opportunityPage.setRedirect(true);
return opportunityPage;
}
public PageReference save()
{
update invoice;
return null;
}
}

 

make sure the variable id you pass in URL is not null.

mohimohi

when you direct bind an object instance using property to visualforce page and if  instance get null value than it through

exception,now for this use list type and than bind using property, nullPointer Exception will remove. Or Use property

Bhawani SharmaBhawani Sharma

1.Change your variable name from id to idVal or something else. Id is a reserved keyword in salesforce.

2.Remove your getInvoice() method as you already have a getter property public Invoice__c invoice{ get; set; }.

3.Put the logic of getInvoice() method to constructor.

NK123NK123
Where is your line 47? I don't see it in your sample code: Put all relevent code and then you can get proper help.
Class.newOpportunityController11.save: line 47, column 8 External entry point
dotnet developedotnet develope

Hi Friend,

 

I can see the code well written, but to run this code sucessfully you need to some chages on it.

 

first

    define standardcontroller attribute on page tag ie "standandcontroller=Invoice__c"

Second

    define extension attribute on page tag ie "extension=newOpportunityController11"

 

thats it, it will work........

 

Thanks & Regards,

 

This was selected as the best answer
Raja JammulaRaja Jammula
Hi 

i have a below code i am getting the error:
System.NullPointerException: Attempt to de-reference a null object at line 66

global class Match implements Database.Batchable<sObject>{
    
   global Database.QueryLocator start(Database.BatchableContext BC){
       
       String query = 'SELECT Name, Id, test__c, opportunity__c, test1__c FROM pro__c ';
       
       return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<pro__c> planProjects){
        Map<String,List<task__c>> taskw = new Map<String,List<task__c>>();
        Map<String,List<milest>> milestW = new Map<String,List<milest>>();
        
           List<String> ppIds = new List<String>();
        for(pro__c pp : planProjects){
            ppIds.add(pp.Id);
        }
       
        List<task__c> Tractas = [SELECT Id,Name,Tracker__r.TrP__r.pro__c,Status__c,
                                                        Comments__c,Completed_Date__c
                                                        FROM  task__c
                                                        WHERE Tracker__r.TrP__r.pro__c in :ppIds];
        
        for(task__c tt : Tractas){
            List<task__c> taskl = new List<task__c>();
            if(taskw.containsKey(tt.Tracker__r.TrP__r.pro__c)){
                
                taskl = taskw.get(tt.Tracker__r.TrP__r.pro__c);    
            }
            taskl.add(tt);
            taskw.put(tt.Tracker__r.TrP__r.pro__c,taskl);
        }
        
        List<milest> milest= [SELECT Id,Name,Status__c,Comments__c,Date__c,pro__c 
                                                    FROM milest
                                                    WHERE pro__c in :ppIds];
                                                    
        for(milest milestone : milest){
            
            List<milest> mileList = new List<milest>();
            
            if(milestW.containsKey(milestone.pro__c)){
                
                mileList = milestW.get(milestone.pro__c);
                
            }
            
            mileList.add(milestone);
            milestW.put(milestone.pro__c,mileList);
            
        }
       
        List<task__c> tasksToUpdate = new List<task__c>();
        LIst<MilestoneMapping__c> mappingRecords = MilestoneMapping__c.getall().values();
        Map<String,String> milestoneMap = new Map<String,String>();
        
        for(MilestoneMapping__c mappingRec : mappingRecords){
            milestoneMap.put(mappingRec.Task_Name__c, mappingRec.Milestone_Name__c);
        }
       
        for(String ppId : taskw.keyset()){
            
            for(task__c tt : taskw.get(ppId)){
                String milestoneMappinName = milestoneMap.get(tt.Name);
                
                
           ---->     for(milest mile :milestW.get(ppId) ){
                    
                    if(mile.Name == milestoneMappinName){
                        tt.Status__c = mile.Status__c;
                        tt.Comments__c = mile.Comments__c;
                        tt.Completed_Date__c = mile.Date__c;
                        tasksToUpdate.add(tt);
                    }
                }

            }
        }    
       
        if(!tasksToUpdate.isEmpty()){
           update tasksToUpdate;
        }
    }

can anyone please help me to get rid of this error