When I try to load the visualforce page it gives me the error "Attempt to de-reference a null object " I have narrowed it down to the line of code lstNotes.add(new NoteWrapper(lstNotes.size(),new Case_Note__c())); but I cannot figure out how to resolve the issue. When I take this line out the page loads; however, the case field which is a Master-Detail field is not available and the button to add a new case is not available. This is the code I have so far:

----------------------APEX CLASS------------------------

      public class MassAddCaseNotes {
public class NoteWrapper{
    public integer index{get; set;}
    public Case_Note__c cNote{get; set;}
    public NoteWrapper(integer i, Case_Note__c cn){
        index = i;
        cNote = cn;

public List<NoteWrapper> lstNotes{get; set;}
public Case_Note__c Case_Notes {get; set;}
public MassAddCaseNotes (ApexPages.StandardSetController controller) 
    lstNotes.add(new NoteWrapper(lstNotes.size(),new Case_Note__c()));
    Case_Notes = new Case_Note__c(); 
public void addCases(){
    lstNotes.add(new NoteWrapper(lstNotes.size(),new Case_Note__c()));

public PageReference save() 
    List<Case_Note__c> lstNotesToInsert = new List<Case_Note__c>();
    for(NoteWrapper nw: lstNotes){
        nw.cNote.Note__c= Case_Notes.Note__c;
        nw.cNote.Date__c= Case_Notes.Date__c;
    insert lstNotesToInsert;         

    return null;

public PageReference saveandclose() 
    return null;


-----------------VISUAL FORCE PAGE-------------------

<apex:page standardController="Case_Note__c" recordSetVar="MassAddCaseNotes" tabStyle="Case_Note__c" extensions="MassAddCaseNotes">

<apex:sectionHeader title="Mass Add Case Notes" />      
<apex:form > 

<apex:pageBlock title="Case Notes" >
    <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save" status="retrieveSaveStatus" id="btnSave" immediate="false" />
        <apex:commandButton action="{!saveandclose}" value="Save and Close" status="retrieveSaveStatus" id="btnSaveClose" immediate="false" />
    <apex:actionStatus layout="block" startText="Saving notes. . . " stopText="" id="retrieveSaveStatus" startStyle="color:green; font-style:italic" stopStyle="color:black;"/>

    <apex:pageMessages showDetail="false" />
    <apex:outputText value="Enter Notes to submit to cases then select cases the notes will be submitted to." style="font-style:italic" /><br/>             

<apex:pageBlockSection title="Enter Your Notes: " columns="1">
    <apex:outputLabel for="CaseNote"> </apex:outputLabel>
    <apex:inputField style="width:300px;height:100px" value="{!Case_Notes.Note__c}" id="CaseNote"  />

<apex:pageBlockSection title="Select Date: " columns="1">
    <apex:outputLabel for="Date"> </apex:outputLabel>
    <apex:inputField value="{!Case_Notes.Date__c}" id="Date" />

<apex:pageBlockSection title="Select Case: " columns="1">
    <apex:pageblocktable value="{!lstNotes}" var="c">                       
    <apex:column headerValue="Case">
        <apex:inputField value="{!c.cnote.Case__c}" id="case" />
    <apex:column headerValue="Action">
        <apex:commandButton value="Add" action="{!addCases}" rendered="{!IF(c.index == lstNotes.size-1,true,false)}"/>

You are missing a line I think - you need to add something like:

lstNotes = new List<NoteWrapper> ();
  • You are awesome! that worked perfectly. – McCormick32 Sep 30 '14 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.