1

I am using display tag in portals(Struts Portal Framework) deployed in websphere portal, using external paging using value list paging (implement PaginatedList) a strong exception has shown up java.lang.ArithmeticException: divide by zero in the following lines:

     int pageCount = behavioursPaginatedList.getFullListSize() /  Math.max(1,behavioursPaginatedList.getObjectsPerPage());
        if ((behavioursPaginatedList.getFullListSize() % behavioursPaginatedList.getObjectsPerPage()) > 0)
    {
        pageCount++;
    }


FullListSize = 13
ObjectPerPage = 4


Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Sherif Omar
  • 113
  • 2
  • 2
  • 8

1 Answers1

3

There are two places where divide by zero could occur:

int pageCount = behavioursPaginatedList.getFullListSize() / 
    Math.max(1,behavioursPaginatedList.getObjectsPerPage());

In this case, Math.max(1, ...) is guaranteed to deliver a value that is non-zero. So the exception cam't be coming from here

if ((behavioursPaginatedList.getFullListSize() % 
        behavioursPaginatedList.getObjectsPerPage()) > 0)

In this case, if behavioursPaginatedList.getObjectsPerPage() returns zero, then you will get a division by zero error.


The fact that you are getting the exception says that division by zero is occurring, and that behavioursPaginatedList.getObjectsPerPage() is returning zero. You need to find out why that is happening.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks @Stephen C for your reply , I do not know how getObjectsPerPage() get zero since I use debbuger to inspect its value it has FullListSize = 13 ObjectPerPage = 4 – Sherif Omar Jan 26 '12 at 08:44
  • Add a trace print or logger call to your code immediately before the `if` statement. Also check the line numbers in the stack trace to **make sure** you know which statement is throwing the exception. (I suspect that either you are printing the value of `objectsPerPage` from a different object, or something is changing it *after* you print it.) – Stephen C Jan 26 '12 at 10:52
  • Also, beware of memory anomalies (i.e. heisenbugs) caused by incorrect synchronization in a multi-threaded application. One characteristic of such bugs is that the symptoms tend to disappear disappear when you attempt to debug them. – Stephen C Jul 10 '17 at 09:21