Export to GitHub

primefaces - issue #1544

LazyDataTable.setRowIndex throws arithmetic exception: division by 0.


Posted on Dec 9, 2010 by Grumpy Bird

Have you posted your issue to PrimeFaces support forum? (If not please do so before creating an issue otherwise your ticket will not be considered and will be marked as wontfix until it is discussed on the forum.)

Not me, but some people did it: http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=5249 http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=4565 http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=4752

What steps will reproduce the problem? I have a filter form with a button hitting on which reloads part of the page with datatable and creates a new LazyDataModel with a new filter applied. When the page is rendered first time everything is ok. But after a click on the button I have an exception:

java.lang.ArithmeticException: / by zero at org.primefaces.model.LazyDataModel.setRowIndex(LazyDataModel.java:62) [primefaces-2.2.RC2.jar:na] at javax.faces.component.UIData.setRowIndex(UIData.java:448) [jsf-api.jar:2.0.2-FCS] at javax.faces.component.UIData.visitTree(UIData.java:1184) [jsf-api.jar:2.0.2-FCS] at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457) [jsf-api.jar:2.0.2-FCS] at javax.faces.component.UIForm.visitTree(UIForm.java:324) [jsf-api.jar:2.0.2-FCS] at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457) [jsf-api.jar:2.0.2-FCS] at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457) [jsf-api.jar:2.0.2-FCS] at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:223) [jsf-impl.jar:2.0.2-FCS] at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177) [jsf-impl.jar:2.0.2-FCS] at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:131) [jsf-impl.jar:2.0.2-FCS] at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:430) [jsf-impl.jar:2.0.2-FCS] at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143) [jsf-impl.jar:2.0.2-FCS] at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:288) [jsf-api.jar:2.0.2-FCS] at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:288) [jsf-api.jar:2.0.2-FCS] at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199) [jsf-impl.jar:2.0.2-FCS] at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl.jar:2.0.2-FCS] at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110) [jsf-impl.jar:2.0.2-FCS] at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl.jar:2.0.2-FCS] at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) [jsf-api.jar:2.0.2-FCS] at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) [web-core.jar:3.0-b74b] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) [web-core.jar:3.0-b74b] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215) [web-core.jar:3.0-b74b] at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79) [primefaces-2.2.RC2.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) [web-core.jar:3.0-b74b] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215) [web-core.jar:3.0-b74b]

Which JSF implementation with version are you using?(Mojarra or MyFaces) mojarra 2.0.2

Which component libraries are you using in addition to PrimeFaces?

Which view technology are you using?(JSP or Facelets) Facelets.

Which application server or servlet container are you using? GlassfishV3

Comment #1

Posted on Dec 9, 2010 by Grumpy Bird

Ups. Forgot to change Type from NewFeature to Defect.

Comment #2

Posted on Dec 9, 2010 by Grumpy Bird

Sorry, forgot to mention, while people say in the forum threads that changing bean visibility scope from Request to View can help, I'm still able to reproduce the error with the Session scope (in terms of CDI).

Comment #3

Posted on Dec 10, 2010 by Grumpy Dog

This also occurs when parent components rendered attribute evaluated as false.

Comment #4

Posted on Dec 22, 2010 by Happy Kangaroo

Additionally, there is a thread in the forum with a detailed explanation. Here's the link http://primefaces.prime.com.tr/forum/viewtopic.php?f=3&t=4956&p=27881#p27881

Comment #5

Posted on Mar 31, 2011 by Massive Camel

I'm still experiencing this problem with PF 2.2.1 with view scoped beans. The first page of data loads in the table, but I get the exception when I click for another page I get the exception. Calling model.setPageSize(n) does prevent the problem but then of course you can't give the user any options as to how many items to display on a single page.

Any chance of getting this in 3.0?

Cheers, Brendan.

Comment #6

Posted on Jul 27, 2011 by Swift Rhino

Taking ownership, set milestone as M3, will be looked in during LazyDataModel API maintenance.

Comment #7

Posted on Aug 26, 2011 by Swift Rhino

Issue 2255 has been merged into this issue.

Comment #8

Posted on Aug 27, 2011 by Swift Rhino

Let's see.

Comment #9

Posted on Aug 27, 2011 by Swift Rhino

(No comment was entered for this change.)

Comment #10

Posted on Aug 31, 2011 by Swift Rhino

ok - sorry, but this has not been fixed!

Your code: public void setRowIndex(int rowIndex) { this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize); }

And the "rowIndex % pageSize" causes still a division by 0 exception when pageSize == 0!

Please just make an IF pageSize != 0 - you can not perform a modulo operation with zero!

Comment #11

Posted on Oct 3, 2011 by Swift Rhino

Issue 2219 has been merged into this issue.

Comment #12

Posted on Oct 9, 2011 by Happy Bird

Comment deleted

Comment #13

Posted on Oct 9, 2011 by Happy Bird

Used the latest jar as on Oct 04 (http://repository.primefaces.org/org/primefaces/primefaces/3.0.M4-SNAPSHOT/) and still this issue exists.

Getting the below error java.lang.ArithmeticException: / by zero

Comment #14

Posted on Oct 10, 2011 by Swift Lion

Reopened by still occurred with 3.0M4.

Comment #15

Posted on Oct 10, 2011 by Swift Lion

Revised within r5573. Thanks for feedback.

Comment #16

Posted on Oct 14, 2011 by Massive Lion

I have just downloaded latest 3.0M4 version and I'm still able to reproduce this problem. In my case I have lazyloaded datatable in outer xhtml document and I'm including it to page:

Comment #17

Posted on Oct 27, 2011 by Swift Rhino

(No comment was entered for this change.)

Comment #18

Posted on Nov 10, 2011 by Happy Bird

This error can be reproduced easily.Just have a dataTable backed by a Lazy Data Model as below. Clicking on the Edit (h:commandLink) more then once produces the error

              <p:column headerText="Id">  
                <h:outputText value="#{q.id}" />  
            </p:column>  
            <p:column headerText="Description">  
                <h:outputText escape="false" value="#{q.description}" />  
            </p:column>  

            <p:column headerText="Edit">  
                <h:commandLink   action="#{searchMultipleChoiceQuestionAction.edit(q)}" target="_blank">
                    <h:graphicImage value="#{messages['cdncontextroot']}/img/edit.png" style="border:none;"/>
                </h:commandLink>
            </p:column>  


        </p:dataTable>

Comment #19

Posted on Nov 10, 2011 by Happy Bird

The issue exists in 3.0M4 release

Comment #20

Posted on Nov 23, 2011 by Quick Monkey

It does exist, please fix it asap.

Comment #21

Posted on Dec 2, 2011 by Quick Giraffe

* FIX/WORKAROUND is working for me! PLEASE READ! *

  1. Prior to today, I had BalusC's PostRedirectGetListener (phaseListener) added to my webapp configuration (faces-config.xml). I was NOT experiencing this "java.lang.ArithmeticException: / by zero" defect while I use LazyDataModel throughout my web app.

http://balusc.blogspot.com/2007/03/post-redirect-get-pattern.html

  1. Today, I decided to remove BalusC's phaseListener (since it was designed for JSF 1.2, and I realized that I don't need it, since I'm at JSF 2.x / Mojarra 2.1.2) and make some changes to clean up my server.log file from WARNING and other messages like Cannot find component, ...

  2. After removing BalusC's phaseListener, I experienced this "java.lang.ArithmeticException: / by zero" defect. So, the thought came to me, PostRedirectGetListener allowed my AJAX requests to work-as-designed, but I had to modify the phaseListener to allow p:schedule to work as designed. :)

  3. I UNCOMMENTED the following in my web.xml (which I had earlier, but commented out when I was debugging and making changes to my app to make p:schedule work as designed):

    javax.faces.PARTIAL_STATE_SAVING true javax.faces.FULL_STATE_SAVING_VIEW_IDS /index.xhtml

  4. I already had the following in my web.xml:

    javax.faces.STATE_SAVING_METHOD server

To clarify #4 above, my entire app has a ui:include src="#{pageNavigation.page}" in /index.xhtml, and all of my pages are displayed by this tag via AJAX and non-AJAX requests. My experience, if you don't set FULL_STATE_SAVING_VIEW_IDS (above in web.xml), then you may experience a faces exception since javax.faces.PARTIAL_STATE_SAVING = true, they work together; i experienced that exception minutes ago until I assigned /index.xhtml to FULL_STATE_SAVING_VIEW_IDS.

I hope this serves as a workaround for others as well. Evidently, many are experiencing this exception.

Comment #22

Posted on Dec 7, 2011 by Grumpy Wombat

Since yesterday it's also in 3.0.RC1-SNAPSHOT

Comment #23

Posted on Dec 19, 2011 by Quick Giraffe
  1. I'm using 3.0.RC1
  2. After I started using 3.0.RC1, I was not experiencing this issue with the majority of the xhtml/jsf pages in the web app that implement LazyDataModel
  3. I added some UI controls such as p:autoComplete and A,B,C,D,E,F... to filter p:dataTable to an xhtml/jsf page that is currently under development, and I experienced this issue today.
  4. After reading this issue (again), I saw someone recommended a solution, so I added the following to my LazyDataModel descendant object:

    @Override public void setRowIndex(int rowIndex) { /* * The following is in ancestor (LazyDataModel): * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize); */ if (rowIndex == -1 || getPageSize() == 0) { super.setRowIndex(-1); } else super.setRowIndex(rowIndex % getPageSize()); }

  5. Problem solved, and I'm not experiencing that error any longer.

  6. It might be best to add this code to all LazyDataModel descendants in my web app.

Comment #24

Posted on Mar 17, 2012 by Grumpy Dog

I encountered the same problem when using Primefaces 3.1.1 and CDI(and MyFaces CODI), I have tried the solution above the problem is still there. I have set the "rows" attribute in the p:dataTable, but I always got pageSize=0 in the lazyDataModel.

Comment #25

Posted on Mar 17, 2012 by Grumpy Dog

BTW, I also used Primefaces 3.2-SNAPSHOT in my another project, it worked well. I used view scoped(wrote a custom Spring view scope) Spring bean as backend bean.

Comment #26

Posted on Mar 28, 2012 by Helpful Bird

Comment deleted

Comment #27

Posted on Mar 28, 2012 by Helpful Bird

I'm facing the same problem using PF 3.2 Final. I tried the code as Smith has suggested below but it isn't working yet. I use Spring with JSF 2.0.

Comment #28

Posted on Mar 28, 2012 by Quick Giraffe

Comment deleted

Comment #29

Posted on Mar 28, 2012 by Quick Giraffe

Comment deleted

Comment #30

Posted on Mar 28, 2012 by Quick Giraffe

@gilson.d...,

  1. search google for the following:

stackoverflow spring lazydatamodel

  1. That list should contain the following:

http://forum.primefaces.org/viewtopic.php?t=18214

  1. The google search results previewed that the book cases Spring/PrimeFaces sample web app included LazyDataModel. Search that forum topic for the download of the sample web app, and study the LazyDataModel code, update the code in your app accordingly, and reply here and at the forum topic I just copy/pasted above.

  2. See below (was resolved today):

https://jira.springsource.org/browse/SWF-1527

FYI, I am not a Spring user/developer at all.

Comment #31

Posted on Mar 29, 2012 by Helpful Bird

Smith, thanks for your valuable tips, i'll check it.

Comment #32

Posted on Apr 10, 2012 by Happy Hippo

I see this defect is marked 'Fixed', but it is still happening for me under PF 3.0 and PF 3.2. It seems to be some kind of timing issue. In some cases the Primefaces framework is invoking setRowIndex() on the LazyDataModel instance before it has invoked load() or set the 'pageSize' attribute. When this happens, a division by zero exception occurs due to pageSize still having the initial value of zero. I deleted chunks of my Facelet page to try and diagnose the problem and at some point, the problem stopped happening. I don't think there was a defect in my Facelet code, but I suspect the change I made caused the JSF view to render things in a different order. At the moment I don't have any sample code that shows how to easily reproduce the problem.

Comment #33

Posted on Apr 10, 2012 by Quick Giraffe

Many have expressed their opinion that this defect is not fixed. The code used by showcase is not always supported. I have shared my workarounds in the community forum, you can search for that if you are interested.

We are developers, we are using java, you can handle the exception by writing your own exception handler. I shared how I am handling lazy datamodel exceptions and all my xhtml pages with lazydatamodel are all working as designed and I don't see this exception at all.

Comment #34

Posted on Apr 11, 2012 by Happy Hippo

As it turns out, my problem was caused by this LazyDataModel defect:

http://code.google.com/p/primefaces/issues/detail?id=3258

Comment #35

Posted on May 13, 2012 by Quick Giraffe

It appears the issue remains. I am attempting very basic navigation to and from a page with a table that uses lazyloading, which throws the exception. I am using two pages in place of a tree table due to the lack of pagination on the treetable.

At this point, I am skeptical of our ability to work with large data :/

Comment #36

Posted on Jun 20, 2012 by Happy Monkey

You are sure you added the new laze="true" attribute if you are using lazydatamodel? I received that error if this attribute was not present. Still a error message not related to the fix.

Comment #37

Posted on Jul 6, 2012 by Quick Panda

also be sure to include 'rows' attribute.

Comment #38

Posted on Jul 24, 2012 by Happy Monkey

It was happening to me as well. I had both lazy="true" and rows="10", and wasn't sure why it stopped working.

My bean is @ViewScoped and I use Weld (CDI), and this started happening after I removed seam-faces from my project. I believe that since seam-faces makes @ViewScoped works with CDI, removing it caused the problem. Remember that @ViewScoped doesn't work out-of-the-box with CDI.

I added seam-faces back to my project and it started working again, meaning that this ArithmeticException disappeared. So, if you are using @ViewScoped in a CDI environment, try adding seam-faces (and seam-international, because seam-faces needs it) to your dependencies and see if it works.

Comment #39

Posted on Jul 28, 2012 by Swift Panda

Now this works with ViewScoped bean. But unfortunately after two years this problem (Division by zero) still exists with RequestScoped bean, and to solve that I have to explicitly call lazyDataModel.setPageSize(); and set a constant number for page size in my lazyDataModel getter method. I use CDI and it does not work with jsf ViewScoped annotation! Please fix this for RequesScoped too. Here is the link to my sample, Just open it in Netbeans and deploy it to Glassfish to see the error.

Attachments

Comment #40

Posted on Jul 28, 2012 by Quick Bird

We've also hit this issue when we used a visitor to traverse the JSF component tree. As soon as it visits a primefaces DataTable you get the ArithmeticException due to a division by zero.

Comment #41

Posted on Oct 15, 2012 by Helpful Camel

This error still happens with primefaces 3.4 in ConversationScoped and lazy loading.

Comment #42

Posted on Dec 6, 2012 by Massive Rhino

Hello, (Tech. Data: Java 1.6.x, Maven 3.x, JSF 2.1, Primefaces 3.4.2 on JBoss 7.1.1Final)

This error still happens with primefaces 3.4.2 and 3.4.1 in ViewScoped and lazy loading. Solution from smithh03 doesn't help.

I have an exception: java.lang.ArithmeticException: / by zero

Please help me, this issue is a real broker for me.

And thank you for the best Faces-Framework!!! Primefaces is realy cool!!!

Comment #43

Posted on Dec 6, 2012 by Quick Giraffe

It's been nearly a year since I came across this, and I don't work for the same company... but I eventually just dug into the code and fixed the damn thing my self. The folks at PM are busy with mind maps and such.

Comment #44

Posted on Dec 6, 2012 by Happy Panda

@43 can you post a patch for it?

Comment #45

Posted on Dec 6, 2012 by Swift Rhino

PrimeFaces PRO is a term based commercial support service. With the exclusive services of Pro account, you no longer need to post your questions in PrimeFaces community forum and your issues to community issue tracker.

Standard PRO Services

Access to pro.primefaces.org Response at most in 1 business day. Defect patches. Private branch management in case you need. Customized builds. Unlimited number of cases. Remote desktop connection. Conference calls for discussions. High priority to your issues. Non PrimeFaces related general JSF assistance.

New Features

New feature and enhancement requests are not available in standard services and provided via an hour based model instead. When you have a feature request we provide an estimate, if you confirm we deliver your request within an estimated timeframe and deduct the amount of work from your hours. These requests can be;

  • New components.
  • New functionality to existing components.
  • Changing the way a certain functionality is implemented.
  • Proof of Concept implementations of a use case.
  • Code reviews to offer best practices.

Hourly rate is 200 usd and discounts apply on bulk purchases that are more than 50. You can purchase additional hours along with the subscription and also anytime during your subscription period. If your subscription term ends with unused hours, they will be added to your new subscription term in case you extend.

To get a quote, please contact us and mention the total number of developers who will be working in your PrimeFaces projects.

Comment #46

Posted on Dec 6, 2012 by Grumpy Bear

I use always my own DataModel and this works for me:

public abstract class UtilitiesLazyDataModel extends LazyDataModel{

public UtilitiesLazyDataModel() {
}    

@Override
public void setRowIndex(int rowIndex) {
    /*
     * The following is in ancestor (LazyDataModel):
     * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
     */
    if (rowIndex == -1 || getPageSize() == 0) {
        super.setRowIndex(-1);
    }
    else {
        super.setRowIndex(rowIndex % getPageSize());
    }      
}

}

Then use your lazydatamodel class with this:

public class MyDataModel extends UtilitiesLazyDataModel{

//override getRowData and getRowKey

}

Comment #47

Posted on Dec 6, 2012 by Quick Giraffe

I no longer have the code as I no longer work at the company... but it's fairly straight forward, check the value is no 0 before the arithmetic is attempted.

This doesn't actually 'fix' the glitch, it just avoids the exception. In the event that you would have had the divide by zero, you'll instead have undesired behavior.

I wanted to get further into it and just fix it, but was on a sprint.

Good luck!!

Comment #48

Posted on Dec 6, 2012 by Quick Giraffe

Just about a year ago, I replied here with the code that solved the problem for me. Other people here that are waiting for this to be fixed, should read any/all posts and all recommended-fixes, and should try it if they 'really' want this fixed.

PrimeFaces does already soooo much for 'us', so if there is something that is 'not' fixed or not 'usuable', then either 'fix it' or do 'not' use it.

It's just like p:selectOneMenu. Many report defects/issues related to it; since there are so many bugs 'reported' with p:selectOneMenu, then don't use it. That's what I do, I use h:selectOneMenu unconditionally and 'always' in my JSF web app. I don't gripe and complain about p:selectOneMenu is not working. I usually 'always' figure out some type of workaround, and h:selectOneMenu is the workaround I chose for p:selectOneMenu. :)

Comment #49

Posted on Dec 6, 2012 by Quick Giraffe

If you have previously patched the issue, feel free to point it out, but this isn't the place for soap box lectures.

Comment #50

Posted on May 1, 2013 by Happy Monkey

Every time this "error occured" it was my own fault!

Show us your bean and markup! The exception is misleading. In most cases it is caused by not providing a collection for the value attribute of the datatable.

This happens very often when I switch from usage of my custom LazyDataModel to non-lazy behavior and not updating the value attribute from value="#{bean.lazyModel}" to value="#{bean.lazyModel.entities}" where entities is the current collection in my custom data-model.

Comment #51

Posted on May 9, 2013 by Happy Rhino

I'm upgrading from Primefaces 3.3 and had the @Override public void setRowIndex(int rowIndex) modification in a custom base class for some time. I'm trying to remove this (eventually) and what to see how I can make "error occured" be my fault too.!

When you say switch from usage value="#{bean.lazyModel}" to value="#{bean.lazyModel.entities}", please elaborate. Do you mean that when using a lazyModel that is is more like a "lazy cursor" construct? If I have 'value="#{myBean.myLazyModel}"' are you saying I should really use 'value="#{myBean.myLazyModel.myListThatTheLoadMethodKeepsUpdating}"' to bind to 'value' in the datatable instead?

Comment #52

Posted on May 9, 2013 by Happy Monkey

@jasonand...@gmail.com

This was just an example of the only occurences of this "bug" for me.

When using your LazyDataModel are you sure you set 'lazy="true"' too? This is the reason I assume for that bug.

Datatable should implicitly use 'lazy="true"' if a LazyDataModel instance is provided as value attribute!

Comment #53

Posted on May 10, 2013 by Happy Rhino

Thanks, I had already had 'lazy="true"' but an explicitly call to setPageSize in my init (@PostConstruct) method once the LazyDataModel was constructed did the trick.

In most places where I was using a datatable and a lazy model everything has always worked fine. The only place I was getting the "/ by zero" error was on datatables that I had on p:dialogs. I had rows="100" set everywhere but when on a dialog I was getting calls to setRowIndex of LazyDataModel prior to pageSize being set. This caused the "/ by zero" for me until I made the changes mentioned above.

If I have time this weekend I'll try to create a simpler page and recreate this condition there to see whether I can avoid the explicit call. But thanks again for helping me get to a much better place with this.

Comment #54

Posted on Jun 19, 2013 by Happy Panda

"Thanks, I had already had 'lazy="true"' but an explicitly call to setPageSize in my init (@PostConstruct) method once the LazyDataModel was constructed did the trick."

That does the trick! now my code is working too! Thx man!

Comment #55

Posted on Jul 1, 2013 by Massive Monkey

Comment deleted

Comment #56

Posted on Jul 31, 2013 by Quick Elephant

I also had similar problems with PF 3.5, I have 2 different lazyDataModel in 2 different dialog in one form (I used it as lookup dialog). Tried to separate it into different forms and it fixed my problems..

Comment #57

Posted on Oct 1, 2013 by Swift Rhino

See #6081 pls for part 2.

Comment #58

Posted on Nov 12, 2013 by Happy Monkey

I never experienced this error before or it was mostly my own fault for using lazy without LazyDataModel.

But now after some unknown changes that i cannot track down, every lazy table in our backend experiences this problem!

Status: Fixed

Labels:
Priority-Medium Type-Defect TargetVersion-3.0 TargetVersion-3.0.M4