You need to sign in to do that
Don't have an account?
Derek 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
This is the VF page
And this is the controller
Could someone assist with what I am missing
cheers
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
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
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:
It will result in attempt to de-reference a null object error.
We should use it like this:
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:
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
When we use list or set then also we need to allocate memory to list or set like this 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.
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:
It will result in attempt to de-reference a null object error.
We should use it like this:
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:
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
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
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
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 ?
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:
Controller:
I hope it helps you!
Regards,
Khan Anas