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
Derek DolanDerek Dolan 

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

I have been building a VF page that updates child and parent records 

The error that I get back after inputting is
​​​​​​
Visualforce Error
Help for this Page

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!insertChild}' in component <apex:commandButton> in page updatechild: Class.AddingChildController.insertChild: line 23, column 1


User-added image
​​​​​​This is the VF page
 
<apex:page controller="AddingChildController" >
<apex:form >
    <apex:variable var="rowNum" value="{!0}" />
    <apex:pageBlock >
        <apex:variable var="rowNum" value="{!0}" />
        <apex:PageBlockTable value="{!childList}" var="int">
        <apex:facet name="footer">
            <apex:commandLink value="Add" action="{!insertRow}"/>
            </apex:facet>
            <apex:column headerValue="Lead Generator">
                <apex:inputField value="{!int.Lead_Gen__c}"/>                                      
            </apex:column>
            
            
            
            <apex:column headerValue="Monday">
            <apex:inputField value="{!int.Monday__c}"/>
            </apex:column>
            <apex:column headerValue="Tuesday">
            <apex:inputField value="{!int.Tuesday__c}"/>
            </apex:column>
            <apex:column headerValue="Wednesday">
            <apex:inputField value="{!int.Wednesday__c}"/>
            </apex:column>
            <apex:column headerValue="Thursday">
            <apex:inputField value="{!int.Thursday__c}"/>
            </apex:column>
            <apex:column headerValue="Friday">
            <apex:inputField value="{!int.Friday__c}"/>
            </apex:column>
             <apex:column headerValue="Delete">
            <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}">
                <apex:param value="{!rowNum}" name="index"/>
                </apex:commandLink>
                <apex:variable var="rowNum" value="{!rowNum+1}"/>
            </apex:column>
        </apex:PageBlockTable>
    <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!insertChild}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>

  </apex:form>
</apex:page>

And this is the controller

 
public class AddingChildController {
Id parentId;
public List<Time_Sheets__c> childList {get;set;}
public Integer rowNum{get;set;}

public Lead_Gen__c Parent {get;set;}
 
 
 


public AddingChildController(){
    Id childId = ApexPages.currentPage().getParameters().get('childId');
    childList = new List<Time_Sheets__c>();
    childList.add(new Time_Sheets__c());
    ParentId=ApexPages.currentPage().getParameters().get('ParentId');   
      
}

public pagereference insertChild(){
    insert childList;

    Parent.Id=parentId;
    update Parent;

    Pagereference page=new pagereference('/'+parentId);
    Return page;

       
}    

public void insertRow(){
    childList.add(new Time_Sheets__c());
    
    
}

public void delRow(){
    rowNum = 
Integer.valueof(apexpages.currentpage().getparameters().get('index'));

childList.remove(rowNum);
}
}

​​​​​​Could someone assist with what I am missing

cheers 
Best Answer chosen by Derek Dolan
Khan AnasKhan Anas (Salesforce Developers) 
Hi Derek,

Greetings to you!

This error occurs when your variable (sObject, List, Set or any other data type) is not initialized (allocated memory). In order to use the non-primitive data type in the code, we need to initialize the memory first. If we don’t do that it may result in Attempt to de-reference a null object error. 

For Example, if you use like this:
Account acc;
acc.Name = 'Khan';

It will result in attempt to de-reference a null object error.

We should use it like this:
Account acc = new Account();
acc.Name = 'Khan';

Reference: http://www.sfdcpoint.com/salesforce/system-nullpointerexception-attempt-to-de-reference-a-null-object/

In your code, you need to initialize the Parent in the constructor:
Parent = new Account();

Please try below code:
public class AddingChildController {
Id parentId;
public List<Time_Sheets__c> childList {get;set;}
public Integer rowNum{get;set;}

public Lead_Gen__c Parent {get;set;}
 
 
 


public AddingChildController(){
    Parent = new Account();
    Id childId = ApexPages.currentPage().getParameters().get('childId');
    childList = new List<Time_Sheets__c>();
    childList.add(new Time_Sheets__c());
    ParentId=ApexPages.currentPage().getParameters().get('ParentId');   
      
}

public pagereference insertChild(){
    insert childList;

    Parent.Id=parentId;
    update Parent;

    Pagereference page=new pagereference('/'+parentId);
    Return page;

       
}    

public void insertRow(){
    childList.add(new Time_Sheets__c());
    
    
}

public void delRow(){
    rowNum = 
Integer.valueof(apexpages.currentpage().getparameters().get('index'));

childList.remove(rowNum);
}
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Team NubesEliteTeam NubesElite
Hi Derek,
When we use list or set then also we need to allocate memory to list or set like this
List<Account> accList = new List<Account>();
Set<String> strSet= new Set<String>();
Please allocate memory to ParentId line no: 2 in controller.


Thank You
www.nubeselite.com
Developement | Training | Consulting


Please Mark this as solution if your problem resolved.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Derek,

Greetings to you!

This error occurs when your variable (sObject, List, Set or any other data type) is not initialized (allocated memory). In order to use the non-primitive data type in the code, we need to initialize the memory first. If we don’t do that it may result in Attempt to de-reference a null object error. 

For Example, if you use like this:
Account acc;
acc.Name = 'Khan';

It will result in attempt to de-reference a null object error.

We should use it like this:
Account acc = new Account();
acc.Name = 'Khan';

Reference: http://www.sfdcpoint.com/salesforce/system-nullpointerexception-attempt-to-de-reference-a-null-object/

In your code, you need to initialize the Parent in the constructor:
Parent = new Account();

Please try below code:
public class AddingChildController {
Id parentId;
public List<Time_Sheets__c> childList {get;set;}
public Integer rowNum{get;set;}

public Lead_Gen__c Parent {get;set;}
 
 
 


public AddingChildController(){
    Parent = new Account();
    Id childId = ApexPages.currentPage().getParameters().get('childId');
    childList = new List<Time_Sheets__c>();
    childList.add(new Time_Sheets__c());
    ParentId=ApexPages.currentPage().getParameters().get('ParentId');   
      
}

public pagereference insertChild(){
    insert childList;

    Parent.Id=parentId;
    update Parent;

    Pagereference page=new pagereference('/'+parentId);
    Return page;

       
}    

public void insertRow(){
    childList.add(new Time_Sheets__c());
    
    
}

public void delRow(){
    rowNum = 
Integer.valueof(apexpages.currentpage().getparameters().get('index'));

childList.remove(rowNum);
}
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Derek DolanDerek Dolan
@khan

thanks for this

I am not getting 

​​​​​​
Visualforce Error
Help for this Page
System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Error is in expression '{!insertChild}' in component <apex:commandButton> in page updatechild: Class.AddingChildController.insertChild: line 25, column 1
Class.AddingChildController.insertChild: line 25, column 1

When I hit save any ideas please ?

Cheers
Khan AnasKhan Anas (Salesforce Developers) 
Derek, I request you to be more clear on your requirement so that we can understand better and can help you accordingly. Please add additional details to highlight exactly what you need.
Derek DolanDerek Dolan
This controller will create the records into the system Succsfully 
 
public class AddingChildController {
Id parentId;
public List<Time_Sheets__c> childList {get;set;}
public Integer rowNum{get;set;}

public AddingChildController(){
    Id childId = ApexPages.currentPage().getParameters().get('childId');
    childList = new List<Time_Sheets__c>();
    childList.add(new Time_Sheets__c());
}


public void insertChild(){
    insert childList;
}

public void insertRow(){
    childList.add(new Time_Sheets__c());
}

public void delRow(){
    rowNum = 
Integer.valueof(apexpages.currentpage().getparameters().get('index'));
    childList.remove(rowNum);
}
}



I only added the parent.id to use with pagerefernce because I need to display the updated records to the user once they hit the save button. With the above controller i just get "URL No Longer Exists
You have attempted to reach a URL that no longer exists on salesforce.com."

So then my question is what do I need to add to the above controller to simply display the updated records to the user ?

thanks 

 
Derek DolanDerek Dolan
The idea is that users can enter values into the VF page and they will be able to creaet many child records agains the parent with the most recent control this works well . But there was no follow up page after the users hit save, that is why I was addingpublic pagereference insertChild(){
 insert childList;

 Parent.Id=parentId;
 update Parent;

  Pagereference page=new pagereference('/'+parentId);
 Return page;

which is where thinge fell apart for me let me knwo if still not clear  ?
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Derek,

As you are inserting multiple records in visualforce page it's not possible to open a detail page. You can display the recently inserted records in a table.

Visualforce:
<apex:page controller="AddingChildController" >
<apex:form >
    <apex:variable var="rowNum" value="{!0}" />
    <apex:pageBlock >
        <apex:variable var="rowNum" value="{!0}" />
        <apex:PageBlockTable value="{!childList}" var="int">
        <apex:facet name="footer">
            <apex:commandLink value="Add" action="{!insertRow}"/>
            </apex:facet>
            <apex:column headerValue="Lead Generator">
                <apex:inputField value="{!int.Lead_Gen__c}"/>                                      
            </apex:column>
            
            
            
            <apex:column headerValue="Monday">
            <apex:inputField value="{!int.Monday__c}"/>
            </apex:column>
            <apex:column headerValue="Tuesday">
            <apex:inputField value="{!int.Tuesday__c}"/>
            </apex:column>
            <apex:column headerValue="Wednesday">
            <apex:inputField value="{!int.Wednesday__c}"/>
            </apex:column>
            <apex:column headerValue="Thursday">
            <apex:inputField value="{!int.Thursday__c}"/>
            </apex:column>
            <apex:column headerValue="Friday">
            <apex:inputField value="{!int.Friday__c}"/>
            </apex:column>
             <apex:column headerValue="Delete">
            <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}">
                <apex:param value="{!rowNum}" name="index"/>
                </apex:commandLink>
                <apex:variable var="rowNum" value="{!rowNum+1}"/>
            </apex:column>
        </apex:PageBlockTable>
    <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!insertChild}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>

    <apex:pageBlock>
        	<apex:pageBlockTable value="{!insertedRecords}" var="rec">
            	<apex:column value="{!rec.Lead_Gen__c}" />
                <apex:column value="{!rec.Monday__c}" />
                <apex:column value="{!rec.Tuesday__c}" />
                <apex:column value="{!rec.Wednesday__c}" />
                <apex:column value="{!rec.Thursday__c}" />
                <apex:column value="{!rec.Friday__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>

  </apex:form>
</apex:page>



Controller:
public class AddingChildController {

public List<Time_Sheets__c> childList {get;set;}
public Integer rowNum{get;set;}
public List<Time_Sheets__c> insertedRecords {get;set;}

public AddingChildController(){
    insertedRecords = new List<Time_Sheets__c>();
    childList = new List<Time_Sheets__c>();
    childList.add(new Time_Sheets__c());
}

public void insertChild(){
    insert childList;
    insertedRecords = [SELECT Id, Lead_Gen__c, Monday__c, Tuesday__c, Wednesday__c, Thursday__c, Friday__c FROM Time_Sheets__c ORDER BY CreatedDate DESC LIMIT 10];
    childList.clear();
    childList.add(new Time_Sheets__c()); 
}    

public void insertRow(){
    childList.add(new Time_Sheets__c());   
}

public void delRow(){
    rowNum = 
Integer.valueof(apexpages.currentpage().getparameters().get('index'));

childList.remove(rowNum);
}
}

I hope it helps you!

Regards,
Khan Anas