2

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;
        lstNotesToInsert.add(nw.cNote); 
    }
    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:pageBlockButtons>                                        
    <apex:actionStatus layout="block" startText="Saving notes. . . " stopText="" id="retrieveSaveStatus" startStyle="color:green; font-style:italic" stopStyle="color:black;"/>

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

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

<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>
    <apex:column headerValue="Action">
        <apex:commandButton value="Add" action="{!addCases}" rendered="{!IF(c.index == lstNotes.size-1,true,false)}"/>
    </apex:column>                                 
    </apex:pageblocktable>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>      
</apex:page>

1 Answer 1

3

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

lstNotes = new List<NoteWrapper> ();
1
  • You are awesome! that worked perfectly. Sep 30, 2014 at 22:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .