1

I've tried to make a button that would add another row to apex:repeat that has couple apex:inputField fields in it, but with no success (i'm getting "Attempt to de-reference a null object" error)

I've created a List of objects in controller and with my method I'm adding new elements to it (which should increase row count on page), I was thinking about rerender, but even then I shouldn't be getting null reference error.

I'm pretty sure that this should be possible and I'm missing something simple.

Here's code sample:

<apex:page controller="CostInvoiceController" docType="html-5.0" showHeader="false" sidebar="false" standardStylesheets="false">
    <apex:stylesheet value="{!URLFOR($Resource.SLDS220,'assets/styles/salesforce-lightning-design-system-vf.css')}"/>
    <html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en"/>
    <div class="clorce" name="Button List">
        <div class="slds-grid slds-grid--align-center slds-text-align--center">
            <div class="slds-grid slds-wrap slds-grid--pull-padded slds-m-top--medium slds-m-bottom--medium">
                <apex:form >
                <table class="slds-table slds-table--bordered slds-table--cell-buffer">
                    <thead>
                        <tr class="slds-text-title--caps">
                            <th scope="col">
                                <div class="slds-truncate" title="Cost Name">Cost Name</div>
                            </th>
                        </tr>
                    </thead>
                    <apex:outputPanel >
                    <apex:repeat value="{!costInvoices}" var="cost" id="theRepeat">

                        <tbody>
                            <tr>
                                <th scope="row" data-label="Cost Name">
                                    <div class="slds-truncate" title="Date"><apex:inputField value="{!cost.Data_platnosci__c}" styleClass="slds-input"/>    </div>
                                </th>
                            </tr>
                        </tbody>
                    </apex:repeat>  
                        </apex:outputPanel>
                </table><br/>
                <apex:commandButton value="Dodaj line item" action="{!addInvoice}" styleclass="slds-button slds-button--brand"/> 
                </apex:form>
            </div>
        </div>
    </div>
</apex:page>

Controller:

public class CostInvoiceController {
    public List<Cost_Invoice__c> costInvoices {get; set;}

   public void addInvoice() 
   {
       Cost_Invoice__c NewCI = new Cost_Invoice__c(Data_wystawienia_faktury__c =date.today() ,Data_platnosci__c = date.today());
   costInvoices.add(NewCI);
   }

}
2
  • 6
    Could you please share your code? It'd be a lot easier if we could see your work so far. Commented Jan 9, 2017 at 15:53
  • It's nothing big, but here it is. (note that this is not finished so don't be bothered by method naming and such :)) Commented Jan 10, 2017 at 8:29

1 Answer 1

2

Basically, whenever you have a variable, you need to initialize it. Since you failed to do so, you got the NullPointerException. To resolve this, you need to add a no-args constructor in order to initialize the list:

public class CostInvoiceController {
    public List<Cost_Invoice__c> costInvoices {get; set;}
    public CostInvoiceController() {
        costInvoices = new List<Cost_Invoice__c>();
    }
    public void addInvoice() 
    {
        Cost_Invoice__c NewCI = new Cost_Invoice__c(Data_wystawienia_faktury__c =date.today() ,Data_platnosci__c = date.today());
        costInvoices.add(NewCI);
    }

}
1
  • I knew it was something small :D Thank a lot for help, this resolved my problem. Commented Jan 10, 2017 at 8:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.