Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to make sum of a column in a af:table

Tarek FathyFeb 20 2012 — edited Feb 28 2012
Dear all
I have a af:table that displays information about Employee vacation. I want to sum the value of the column "ActualNumberOfDays".The property "RangeSize" is set to 25.
Hrere is the table
<af:table value="#{bindings.empVacations1.collectionModel}"
                            var="row" rows="#{bindings.empVacations1.rangeSize}"
                            emptyText="#{bindings.empVacations1.viewable ? 'No data to display.' : 'Access Denied.'}"
                            fetchSize="#{bindings.empVacations1.rangeSize}"
                            rowBandingInterval="0"
                            filterModel="#{bindings.empVacations1Query.queryDescriptor}"
                            queryListener="#{bindings.empVacations1Query.processQuery}"
                            filterVisible="true" varStatus="vs"
                            selectedRowKeys="#{bindings.empVacations1.collectionModel.selectedRow}"
                            selectionListener="#{bindings.empVacations1.collectionModel.makeCurrent}"
                            rowSelection="single" id="t3"
                            columnStretching="column:c1">
                    <af:column sortProperty="NameAr" filterable="true"
                               sortable="true"
                               headerText="#{viewcontrollerBundle.VACATION_TYPE}"
                               id="c1">
                      <af:outputText value="#{row.NameAr}" id="ot2"/>
                    </af:column>
                    <af:column sortProperty="StartDate" filterable="true"
                               sortable="true"
                               headerText="#{viewcontrollerBundle.START_DATE}"
                               id="c7">
                      <f:facet name="filter">
                        <af:inputDate value="#{vs.filterCriteria.StartDate}"
                                      id="id1"/>
                      </f:facet>
                      <af:outputText value="#{row.StartDate}" id="ot5">
                        <af:convertDateTime pattern="#{bindings.empVacations1.hints.StartDate.format}"/>
                      </af:outputText>
                    </af:column>
                    <af:column sortProperty="SetEndDate" filterable="true"
                               sortable="true"
                               headerText="#{viewcontrollerBundle.END_DATE}"
                               id="c8">
                      <f:facet name="filter">
                        <af:inputDate value="#{vs.filterCriteria.SetEndDate}"
                                      id="id3"/>
                      </f:facet>
                      <af:outputText value="#{row.SetEndDate}" id="ot8">
                        <af:convertDateTime pattern="#{bindings.empVacations1.hints.SetEndDate.format}"/>
                      </af:outputText>
                    </af:column>
                    <af:column sortProperty="Holidays" filterable="true"
                               sortable="true"
                               headerText="#{viewcontrollerBundle.HOLYDAYS_COUNT}"
                               id="c9">
                      <af:outputText value="#{row.Holidays}" id="ot10">
                        <af:convertNumber groupingUsed="false"
                                          pattern="#{bindings.empVacations1.hints.Holidays.format}"/>
                      </af:outputText>
                    </af:column>
                    <af:column sortProperty="ActualNumberOfDays"
                               filterable="true" sortable="true"
                               headerText="#{viewcontrollerBundle.ACTUAL_DAYS}"
                               id="c6">
                      <af:outputText value="#{row.ActualNumberOfDays}" id="ot9">
                        <af:convertNumber groupingUsed="false"
                                          pattern="#{bindings.empVacations1.hints.ActualNumberOfDays.format}"/>
                      </af:outputText>
                    </af:column>
                  </af:table>
To do this I added an new attribute "totalVacationDays"in the managed bean
private int totalVacationDays;
In the "getTotalVacationDays" I wrote this code to loop through the table and to sum the attribute "ActualNumberOfDays"
 public int getTotalVacationDays() {
        totalVacationDays = 0;
        oracle.jbo.domain.Number total;
        int rowcount = 0;
        DCBindingContainer bc =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iter =
            bc.findIteratorBinding("empVacations1Iterator");
        RowSetIterator rowset = iter.getRowSetIterator();
        rowcount = rowset.getRowCount();
        for (int i = 0; i < rowcount; i++) {
            Row currentRow = iter.getRowAtRangeIndex(i);
            total =
                    (oracle.jbo.domain.Number)currentRow.getAttribute("ActualNumberOfDays");
            totalVacationDays += total.intValue();
        }
        return totalVacationDays;
    }
I added an af:output field in the page and set the value to "totalVacationDays"
                     <af:outputText id="ot38"
                                     inlineStyle="font-weight:bold;"
                                     partialTriggers="t3"
                                     value="Total:  #{common.totalVacationDays}"/>
I run the page . I have a log in page to enter id and password. The employee Tom only has 10 Records in the vacation table and the "totalVacationDays" is displayed correctly.
I run the page again . I log in with employee ALAN. The employee ALAN has 35 records in the Vacation table. the "totalVacationDays" is not displayed and I got the following error:
javax.el.ELException: java.lang.NullPointerException
	at javax.el.BeanELResolver.getValue(BeanELResolver.java:266)
	at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:173)
	at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:200)
         .....
Caused by: java.lang.NullPointerException
	at org.dhaman.es.bean.DhamanCommon.getTotalVacationDays(DhamanCommon.java:621)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
I run again In debug mode and I notice that if the records retrieved are more than 25 this error is raised.The error is raised after record 25 exactly. Is this related to the table property "RangeSize" which is set to 25.
I tried to change it to 500 but still the same error occurred.
Please Help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2012
Added on Feb 20 2012
15 comments
1,712 views