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
Natalie Jane HodsonNatalie Jane Hodson 

Error in expression when Dynamically Adding Rows

Hi I have a Controller that dynamically adds Rows and new records through a Visual Force page. I had the page working perfectly as a detail button, however when I added in the elements to make it a list button, I recieve the following error on  Add Row execution:

Attempt to de-reference a null object
Error is in expression '{!addRows}' in component <apex:commandButton> in page resource_allocation: Class.ManageListController.addRows: line 46, column 1


Not sure what has changed since adding the List Button lines as the liones below haven't altered. Any help would be greatly appreciated!

Controller
public class ManageListController 
{
   private final Resource_Allocation__c RA;
     // The extension constructor initializes the private member
        // variable RA by using the getRecord method from the standard
        // controller.
        public ManageListController(ApexPages.StandardSetController stdController) {
            this.RA = (Resource_Allocation__c)stdController.getRecord();
        }

 public List<ResourceAllocationWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
  
 public ManageListController()
 {
  wrappers=new List<ResourceAllocationWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers.add(new ResourceAllocationWrapper(nextIdent++));
  }
 }
  
 public void delWrapper()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
 }
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new ResourceAllocationWrapper(nextIdent++));
  }
 }
  
 public PageReference save()
 {
  List<Resource_Allocation__c> RA=new List<Resource_Allocation__c>();
  for (ResourceAllocationWrapper wrap : wrappers)
  {
   RA.add(wrap.RA);
  }
   
  insert RA;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('Opportunity').getDescribe().getKeyPrefix() + '/o');
 }
  
 public class ResourceAllocationWrapper
 {
  public Resource_Allocation__c RA {get; private set;}
  public Integer ident {get; private set;}
   
  public ResourceAllocationWrapper(Integer inIdent)
  {
   ident=inIdent;
   RA=new Resource_Allocation__c(Name='RA' + ident);
  }
 }
}

VisualForce Pagew
<apex:page standardcontroller="Resource_Allocation__c" recordSetVar="Resource Allocations" tabstyle="Resource_Allocation__c" extensions="ManageListController" >
<apex:form >
   <apex:pageBlock title="Bulk Resource Allocation Create">
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
        <apex:column headervalue="Opportunity">
        <apex:inputField value="{!wrapper.RA.Opportunity__c}"/>
        </apex:column>
        <apex:column headerValue="Resource Name or Role">
         <apex:inputField value="{!wrapper.RA.Resource_Name_or_Role__c}"/>
                 </apex:column>
        <apex:column headerValue="Start">
            <apex:inputField value="{!wrapper.RA.Start_Date__c}"/>
        </apex:column>
         <apex:column headerValue="End">
            <apex:inputField value="{!wrapper.RA.End_Date__c}"/>
         </apex:column>
         <apex:column headerValue="Est Pay">
            <apex:inputField value="{!wrapper.RA.Resource_Est_Pay__c}"/>
         </apex:column>
         <apex:column headerValue="Est Charge">
            <apex:inputField value="{!wrapper.RA.Resource_Est_Charge__c}"/>
         </apex:column>
        <apex:column headerValue="Max Days Contracted">
        <apex:inputfield value="{!wrapper.RA.Max_Days_Contracted__c}"/>
               </apex:column>
         <apex:column headervalue="Daily Expense Allowance">
               <apex:inputField value=" {!wrapper.RA.Daily_Expense_Allowance__c}"/>
               </apex:column>
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
            </apex:commandButton>
         </apex:column>
      </apex:pageBlockTable>
      <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/>
      </apex:commandButton>
      <apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="5" assignTo="{!addCount}"/>
      </apex:commandButton>
      <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageBlock>
 </apex:form>
</apex:page>

(Code credit to http://bobbuzzard.blogspot.com/2011/07/managing-list-of-new-records-in.html)

 
Best Answer chosen by Natalie Jane Hodson
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Salesforce Admin

Add wrappers=new List<ResourceAllocationWrapper>(); to the parameterized constructor as well.

Cheers!!!

All Answers

Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Salesforce Admin

Add wrappers=new List<ResourceAllocationWrapper>(); to the parameterized constructor as well.

Cheers!!!
This was selected as the best answer
Natalie Jane HodsonNatalie Jane Hodson
Hi Syed,

Thank you for responding.
I am sorry to be a pain, i'm not sure where to insert that line.

Can you please advise? 

Thanks,

Natalie
Natalie Jane HodsonNatalie Jane Hodson
Hi Syed,

Figured it out! Thank you so much for your help!

Thanks,

Natalie