Why I am getting this error "Caused by: java.lang.ArithmeticException: divide by zero"?

Let’s have a look at this line:

int a = Math.round((float)(360/size));

And assume that size==361.

Since size is an int we have an integer-division and 360/size equals 0. Then you cast it to a float resulting in 0.0f, round it (givin 0.0f) and assign it to an int that’s also 0 as a result.

What you probably meant to do is

int a = Math.round(((float)360/size));

Note the different parentheses…

Leave a Comment