简体   繁体   中英

Java Applet Returns “java.lang.ArithmeticException: / by zero”

What I am doing: In the code snippet below I am printing out the X Axis of a bar chart which is dependent on the amount of words of different lengths. Eg the largest word is length 6 so the X axis has 1,2,3,4,5,6, along the bottom.

What the issue is: The code is completely functional, but I am returned an error in my paint method.

What I've tried: Isolating the line of code which is causing the issue, by making the code block into a method which is called in the paint method.

Used Variables:

lengthCountArray.length (the arrays length dictates how many numbers are along the bottom)

int canvasWidth = 410; 
int inferiorLeftCornerX = 50; // this the the x point of the left corner on the x axis
int inferiorLeftCornerY = 470; // this the the y point of the left corner on the x axis
int barWidth = canvasWidth / lengthCountArray.length; // this calculates the distance for the bars
int posLabelX = barWidth / 2; // this puts the label in the centre of the bars by halving it 
posLabelX = inferiorLeftCornerX - posLabelX; // this is so that the initial number isn't indented twice

for (int a = 1; a <  (lengthCountArray.length+1); a++){
    posLabelX = posLabelX + barWidth;
    g.drawString(String.valueOf(a), posLabelX, inferiorLeftCornerY);
}

The line which is causing the problems is "int barWidth = canvasWidth / lengthCountArray.length; // this calculates the distance for the bars" (line 122).

The following errors are returned:

Exception in thread "AWT-EventQueue-1" java.lang.ArithmeticException: / by zero
    at java_assignment.JavaAppletMain.paint(JavaAppletMain.java:122)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
    at sun.lwawt.LWRepaintArea.paintComponent(LWRepaintArea.java:54)
    at sun.awt.RepaintArea.paint(RepaintArea.java:240)
    at sun.lwawt.LWComponentPeer.handleJavaPaintEvent(LWComponentPeer.java:1267)
    at sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1150)
    at java.awt.Component.dispatchEventImpl(Component.java:4937)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I have been trying to fix this for hours, before resulting to StackOverflow, so if you require any more information please do not hesitate to ask!

Thank you for viewing my question.

Although I didn't want to implement random if-statements, I have to counteract the division by 0. The if-statement is as follows:

if (lengthCountArrayLength == 0){
            lengthCountArrayLength = 1;
        }

Thank you for your comments. If you find a better way of doing this please do comment or leave a answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM