function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ss123ss123 

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

 I have a VF page and if I don't enter any values on that I will get following error

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

 

Error is in expression '{!Saveme}' in component <apex:commandButton> in page cancellistingpage


Class.CancelListingPageController.Saveme: line 18, column 1

 

controller as below

 

public class CancelListingPageController {
    
    public String name{get;set;}
    Public String ForgedDocs {get;set;}
    Public String Ids{get;set;}
    Public Listing__c Listing {get;set;}
    Public PageReference Saveme(){
    
    
   Contact  con = [SELECT Id, Name, Contact_Notes__c from Contact Where Id = :Listing.Contact__r.Id];
    Map<Id, Case> caseMap = new Map<Id, Case>([Select Id, Salesforce_Case_Link__c, Subject, 
                                                                  CreatedDate, Status, OwnerId, All_Attachments__c
                                                            from Case 
                                                            where ContactId = :con.Id 
                                                            order by CreatedDate desc]);
    
     
    if(Listing.Cancellation_Reason__c.contains('MISC Other reason') && (Listing.Cancellation_Note__c==null ||Listing.Custom_Note__c==null))
    {
           
           ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Internal Cancellation Note & Custom Adverse Action Note are required');
            ApexPages.addMessage(myMsg);
            return null;
    }
        Update Listing;
        Return new pagereference('/apex/Listing?id='+ids);
    }
    public CancelListingPageController(ApexPages.StandardController controller) {
        ids = controller.getId();
        Listing = [select Cancellation_Note__c,Forged_Docs__c,Status__c,
                            Cancellation_Reason__c,Custom_Note__c,Contact__r.id,Cancellation_Reason_Code__c from Listing__c where id =: ids];
        Listing.Status__c='Cancelled';  
    }
Vinita_SFDCVinita_SFDC

Hello,

 

In your VF page first define standard controller attribute ie "standardcontroller="contact" and second  define extension attribute on page tag ie "extension=CancelListingPageController". Still if it doesn't work then check if you are getting null value anywhere by using system.debug statements.

Abhi_TripathiAbhi_Tripathi

Hey ss123

 

How can you update Listing in saveme() method, were you have dont nothing with it,

 

you should do like this

 

public class CancelListingPageController {

public String name{get;set;}
Public String ForgedDocs {get;set;}
Public String Ids{get;set;}
Public Listing__c Listing {get;set;}

//Constructor
public CancelListingPageController(ApexPages.StandardController controller) {
ids = controller.getId();
Listing__c = new Listing__c();
}

//Method
Public PageReference Saveme(){

//Do this query here
Listing = [select Cancellation_Note__c,Forged_Docs__c,Status__c,
Cancellation_Reason__c,Custom_Note__c,Contact__r.id,Cancellation_Reason_Code__c from Listing__c where id =: ids];

Listing.Status__c='Cancelled';


Contact con = [SELECT Id, Name, Contact_Notes__c from Contact Where Id = :Listing.Contact__r.Id];

Map<Id, Case> caseMap = new Map<Id, Case>([Select Id, Salesforce_Case_Link__c, Subject,
CreatedDate, Status, OwnerId, All_Attachments__c from Case
where ContactId = :con.Id order by CreatedDate desc]);


if(Listing.Cancellation_Reason__c.contains('MISC Other reason') && (Listing.Cancellation_Note__c==null ||Listing.Custom_Note__c==null))
{

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Internal Cancellation Note & Custom Adverse Action Note are required');
ApexPages.addMessage(myMsg);
return null;
}

if(Listing.size() > 0) {
Update Listing;
Return new pagereference('/apex/Listing?id='+ids);
}
}
}