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
Automate ProcessAutomate Process 

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

Hi Team,

I am new to visualforce coding. I was tyrying to do some customization on the case object but whenever I am trying to save the record into the object its throwing me: System.NullPointerException: Attempt to de-reference a null object" error. Any help in this regard is much appreciated.
Below goes by VF page code:
 
<apex:page StandardController="Case" extensions="detailsController" showHeader="False" sidebar="False">
    <apex:form >
    <apex:pagemessages ></apex:pagemessages>
        <apex:pageBlock id="pb" >
            <apex:pageBlockSection title="Please fill in the below details">
                <apex:inputField value="{!c.user_fname__c}"/>
                <apex:inputField value="{!c.user_lname__c}"/>
                <apex:inputField value="{!c.user_alias__c}"/>
                <apex:inputField value="{!c.user_email__c}"/>
                <apex:inputField value="{!c.user_CommunityNickname__c}"/>
                <apex:inputField value="{!c.user_role__c}" required="True"/>
                <apex:inputField value="{!c.user_license__c}" required="True"/>
                <apex:inputField value="{!c.user_profile__c}" required="True"/>
                <apex:inputField value="{!c.user_EmailEncodingKey__c}" required="True"/>
            </apex:pageblocksection>
        </apex:pageBlock>
        <center>
        <apex:commandButton value="Save" action="{!saveRec}"/>
        <apex:commandButton value="Reset All" action="{!resetAll}" immediate="True"/>
        <apex:commandButton value="Cancel & Back to Previous" action="{!returnBack}" immediate="true"/>
        </center>
    </apex:form>
</apex:page>

and the detailsController is as below:
 
<apex:page StandardController="Case" extensions="detailsController" showHeader="False" sidebar="False">
    <apex:form >
    <apex:pagemessages ></apex:pagemessages>
        <apex:pageBlock id="pb" >
            <apex:pageBlockSection title="Please fill in the below details">
                <apex:inputField value="{!c.user_fname__c}"/>
                <apex:inputField value="{!c.user_lname__c}"/>
                <apex:inputField value="{!c.user_alias__c}"/>
                <apex:inputField value="{!c.user_email__c}"/>
                <apex:inputField value="{!c.user_CommunityNickname__c}"/>
                <apex:inputField value="{!c.user_role__c}" required="True"/>
                <apex:inputField value="{!c.user_license__c}" required="True"/>
                <apex:inputField value="{!c.user_profile__c}" required="True"/>
                <apex:inputField value="{!c.user_EmailEncodingKey__c}" required="True"/>
            </apex:pageblocksection>
        </apex:pageBlock>
        <center>
        <apex:commandButton value="Save" action="{!saveRec}"/>
        <apex:commandButton value="Reset All" action="{!resetAll}" immediate="True"/>
        <apex:commandButton value="Cancel & Back to Previous" action="{!returnBack}" immediate="true"/>
        </center>
    </apex:form>
</apex:page>


 
Best Answer chosen by Automate Process
Mahesh DMahesh D
I am guessing you may be declared a Case variable like below:

public Case c {get; set;}

You have to initialize it in the Constructor.

public detailsController(ApexPages.StandardController stdController) {
        c = (Case) stdController.getRecord();
}

Also paste the controller code here if it doesn't work.

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Please paste the controller. You pasted VF page twice.
Mahesh DMahesh D
I am guessing you may be declared a Case variable like below:

public Case c {get; set;}

You have to initialize it in the Constructor.

public detailsController(ApexPages.StandardController stdController) {
        c = (Case) stdController.getRecord();
}

Also paste the controller code here if it doesn't work.

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Automate ProcessAutomate Process
Oops..!! My bad.. Here goes the controller:
 
public with sharing class detailsController {    
    public Case c{get;set;}

    public detailsController(ApexPages.StandardController controller) {
        Case c = new Case();
    }
    
    //Method to reset all the fields
    Public PageReference resetAll(){
            PageReference pageRef = new PageReference('/apex/User_Detail_Page');
            pageRef.setredirect(true);
            return pageRef ;        
    }
    
    //Method to go back to the earlier page
    Public PageReference returnBack(){
            PageReference pageRef = new PageReference('/apex/CloneThisUser');
            pageRef.setredirect(true);
            return pageRef ;        
    }
    
    
    //Method to set the default values required for saving the records
    public void setDefault(){
       c.Origin = 'Web';
       c.user_isActive__c = True;
       c.user_app__c = True;
       c.Subject = 'New User Creation Request';
    }
   
    //Method to save the inserted values
    public void saveRec(){
        setDefault();
        insert c;
    }
}

 
Automate ProcessAutomate Process
Hey Mahesh,
That worked perfectly..!! Thank you so much for your quick help