You need to sign in to do that
Don't have an account?
System.NullPointerException: Attempt to de-reference a null object
Hi,
I am fairly new to salesforce and am making a TimeSheet app.
What I have is a dropdown box with a list of accounts. Once an account is selected it should show a dropdown box that contains a list of projects connected to that account. The accounts dropdown box shows but every time a click on an item in the account I get:
System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!projectsByAccount}' in component <apex:page> in page timesheet Class.TimesheetEntry.projectsByAccount: line 36, column 1
Here is my VisualForce code:
<apex:page controller="TimesheetEntry" sidebar="false"> <apex:pageBlock title="Timesheet Entry"> <apex:pageBlockSection > Resource Name: {!$User.FirstName} {!$User.LastName} </apex:pageBlockSection> <apex:form > <apex:pageBlockSection columns="4"> <apex:pageBlockSectionItem > <apex:selectList value="{!accountID}" size="1"> <apex:selectOptions value="{!accounts}"> </apex:selectOptions> <apex:actionSupport event="onchange" action="{!projectsByAccount}" rerender="projectSectionItem"> <apex:param name="accountid" assignTo="{!accountID}" value="{!accountID}" /> </apex:actionSupport> </apex:selectList> </apex:pageBlockSectionItem> <apex:outputPanel id="projectSectionItem" style="display:{!if(showProjects,'block','none')};"> <apex:selectList value="{!projectID}" size="1"> <apex:selectOptions value="{!projects}"> </apex:selectOptions> </apex:selectList> </apex:outputPanel> </apex:pageBlockSection> </apex:form> </apex:pageBlock> </apex:page>
and here is my Custom Controller code:
public with sharing class TimesheetEntry { // sharing class so that user permissions are respected public String projectID { get; set; } public String accountID { get; set; } public Boolean showProjects { get; set; } public List<SelectOption> projOptions { get; set;} // Constructor public TimesheetEntry() { // Do not show the projects dropdown on page load System.debug('Constructor'); showProjects = false; List<SelectOption> projOptions = new List<SelectOption>(); } // Method referenced in VF page as {!accounts} public List<SelectOption> getAccounts() { System.debug('getAccounts'); List<SelectOption> accOptions = new List<SelectOption>(); accOptions.add(new SelectOption('Select Account', '--Select Account--')); for (Account account: [SELECT id, Name FROM Account]) { accOptions.add(new SelectOption(account.id, account.Name)); } return accOptions; } // Method referenced in VF page as {!projects} public List<SelectOption> getProjects() { System.debug('getProjects'); return projOptions; } // Action call in VF page {!projectsByAccount} referecnces this method public PageReference projectsByAccount() { System.debug('Inside page reference'); projOptions.add(new SelectOption('Select Project', '--Select Project--')); System.debug('Added first item'); for (Project__c project: [SELECT id, Name FROM Project__c WHERE Project__c.Account__r.id =: accountID]) { projOptions.add(new SelectOption(project.id, project.Name)); } return null; } }
The only debug statement that is not displayed in the System log is:
System.debug('Added first item');
so I believe that the problem lies in what happens before this line. Being new to the Salesforce platform I am finding it difficult to work it out.
Thanks in advance.
nav
The error is because you haven't initialized the List projOptions. In your Constructor, you have actually defined a new one with the same name instead of just initializing. So change your constructor code to :
// Constructor
public TimesheetEntry() {
// Do not show the projects dropdown on page load
System.debug('Constructor');
showProjects = false;
projOptions = new List<SelectOption>();
}
This should work. Let me know