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
Ritu Ghosh 13Ritu Ghosh 13 

Null pointer exception while referencing server side controller in lightning component due to which I am getting error "An internal server error has occurred Error ID: 694637652-163917 (119852647)"

While I am trying to Save data in Server side below error is captured:
An internal server error has occurred
Error ID: 694637652-163917 (119852647)
On checking logs there is null pointer exception and I can see regForm instance is holding "NULL" value.Sever side controller null pointer exception
Below is the component and controller code:
Component Code

<aura:component controller="admtest__.SaveRegistrationFormDetails" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"
                access="global" >
    
    <aura:attribute name="abc" type="String"/>
    <aura:attribute name="Name" type="String" default="" />
    <aura:attribute name="ParentName" type="String" default="" />
    <aura:attribute name="StandardOrClass" type="String" default="" />
    <aura:attribute name="NumberOfStudyHours" type="String[]"
                    default=" , Less than an hour ,In between 1 to 4,
                             In between 4 to 8, More than 8  hours" />
    <aura:attribute name="UsersResponse1" type="String" default=""/>
    <aura:attribute name="MobileNumber" type="Integer" default="" />
    <aura:attribute name="RegisteredCourseBefore" type="Boolean" default="false" />
    <aura:attribute name="InterestedInOnlineCourse" type="Boolean" />
    <aura:attribute name="CheckBoxvalue" type="Boolean" default="false"/>
    <aura:attribute name="PageHeading" type="String" default="Registration form for Students"/>
    <aura:attribute name="EducationalDetailList" type="List" default="[]"/>
    <aura:attribute name="RegForm" type="admtest__Registration_Form__c" default="{'sobjectType' : 'admtest__Registration_Form__c'}" />
    <br/><br/>
    
    <div class="slds-page-header">
        <div class="slds-align_absolute-center" style="height: 5rem;">
            <div class="slds-text-heading--large">
                <div class="slds-m-*_large">
        {!v.PageHeading}
                </div>
           </div>
    </div>
    </div>
    
    <div class="slds-size--5-of-8">
    <lightning:input label="Enter your Name" name="StudentName" value="{!v.RegForm.Name}"/>
    <br/>
    <lightning:input label="Enter you Parents name" name="parent" value="{!v.RegForm.admtest__Parent_Name__c}"/>
    <br/>
        <lightning:input label="Enter your Class " name="StandardOrClass" value="{!v.RegForm.admtest__Which_Class__c}"/>
    <br/>
    
    <lightning:select name="numofStudyHrs" label="How many  hours do you Study"
                      value="{!v.RegForm.admtest__Study_Hours__c}">
            <aura:iteration items="{!v.NumberOfStudyHours}" var="hrs">
                <option value="{!hrs}" text="{!hrs}"/>
        </aura:iteration>
    </lightning:select>
    <br/>
    
    <lightning:input name="MobileNumber" label="Phone Number" type="telephone" value="{!v.RegForm.admtest__Mobile_Number__c}"/>
    <br/>
        
        <aura:iteration items="{!v.EducationalDetailList}" var="item" indexVar="index">
        <c:Add_Educational_Details EducationalDetailListInnerComponent="{!v.EducationalDetailList}" IndexNo="{!index}"/>
        </aura:iteration>
     
    <lightning:button iconName="utility:add" variant="border-filled" label="Add Educational Details" onclick="{!c.addDetails}"></lightning:button>
        <br/>        
        <lightning:input type="checkbox" label="Are you available on weekends for Class" name="coursecheckbox" />
 
    <br/>
    <lightning:input aura:id="checkBox" type="checkbox" onchange="{!c.onCheckChange}" label="Are you interested in online course?" name="onlinechk"/>
     <aura:if isTrue="{!v.CheckBoxvalue}">
         <i>Happy Learning!</i>
    </aura:if>   
        
        <br/>
        
    <lightning:button type="Submitt" onclick="{!c.doSubmit}">Submitt</lightning:button>
        </div>
</aura:component>

Controller Code:
({
    doSubmit : function(cmp, evt, hlpr) {
        //call an apex function to store data in reg form
        //pass regform as paramamer which  you  have mentioned in attribute
        var RegForm = cmp.get("v.RegForm");
        var action = cmp.get("c.SaveRegistrationDetail");
        //set the paramamter  value
        action.setParams({ regForm  : RegForm });
        
        //set the callback  function
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert("From Server: " + response.getReturnValue());
                console.log("From Server:" + response.getReturnValue());
                
            }
           else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " +
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
       
        // $A.enqueueAction adds the server-side action to the queue.
       
            }
        });
        $A.enqueueAction(action);  
        
    },
    
    onCheckChange: function(cmp,evt,hlpr) {
    console.log("this is  from controller Function");
        //function defined in helper section;
        
        hlpr.onClickCheckBoxHelper(cmp,event);
       },
    
    addDetails: function(cmp,evt,hlpr){
      console.log("Add educational Details");
      var currentEducationalDetailList = cmp.get("v.EducationalDetailList");
      var currentSize= parseInt((currentEducationalDetailList.length));
      var newSize= parseInt((currentSize.length)+1);
      currentEducationalDetailList.push(newSize);
      cmp.set("v.EducationalDetailList",currentEducationalDetailList);
    }
})
Apex Class:
public class SaveRegistrationFormDetails{
@AuraEnabled
public static id SaveRegistrationDetail(admtest__Registration_Form__c regForm ){
     // DML operation to save RegForm Details   
     insert regForm;
     return regForm.id; 
    
}
}

Are there ant issue with the namespace syntax  that  I  am using ?
It will be helpful if someone can guide me with this issue.
Thanks,
Ritu Ghosh
Sunil MadanaSunil Madana
Hi Ritu,
Is there a special reason for you to use the below;
<aura:attribute name="RegForm" type="admtest__Registration_Form__c" default="{'sobjectType' : 'admtest__Registration_Form__c'}" />
For aura:attribute sobject types, use like below;
<aura:attribute name="RegForm" type="admtest__Registration_Form__c" />
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_attr_types_object_salesforce.htm

You can use the example found in the below URL to solve your issue;
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_records_save.htm

Hope the above solution helps.
Ritu Ghosh 13Ritu Ghosh 13
Thanks Sunll for solution.
When I am trying below :
 <aura:attribute name="RegForm" type="admtest__Registration_Form__c" />

getting below error :
This page has an error. You might just need to refresh it. Error in $A.getCallback() [Cannot read property 'Name' of null] Failing descriptor: {lightning:input}

Input value that I  have mentioned is as below:
<lightning:input label="Enter your Name" name="StudentName" value="{!v.RegForm.Name}"/>
    <br/>

There is some issue with callBack .It will be very helpful if you can guide me further.

Thanks,
Ritu Ghosh
Sunil MadanaSunil Madana
Hi Ritu,
Can you try the below?

Use the below aura:attribute that you used earlier in your code like since it is special reason..
<aura:attribute name="RegForm" type="admtest__Registration_Form__c" default="{'sobjectType' : 'admtest__Registration_Form__c'}" />
Replace below
<lightning:input label="Enter your Name" name="StudentName" value="{!v.RegForm.Name}"/>
with
<force:inputField value="{!v.RegForm.Name}"/>
Ritu Ghosh 13Ritu Ghosh 13
Hi Sunll,
When I tried it with <force:inputField> I am getting below

User-added image
Name is not available now.
When  I  went through lightning component docs below is mentioned.
User-added image
Over here Data is available from SOQL query but  in my  case I  am trying to enter new data and currently there are no  records in db.It  is an insert operation .

In registration form directly I am getting next field and that is Parent's name ( which is  incorporated with lightning:input tag) hence facing same error again
This page has an error. You might just need to refresh it. Error in $A.getCallback() [Cannot read property 'admtest__Parent_Name__c' of null] Failing descriptor: {lightning:input}

How should I  proceed now...

Thanks,
Ritu Ghosh