简体   繁体   English

Java Applet返回“ java.lang.ArithmeticException:/零”

[英]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. 我正在做什么:在下面的代码片段中,我打印出条形图的X轴,该图取决于不同长度的单词的数量。 Eg the largest word is length 6 so the X axis has 1,2,3,4,5,6, along the bottom. 例如,最大的单词是长度6,因此X轴沿底部具有1,2,3,4,5,6。

What the issue is: The code is completely functional, but I am returned an error in my paint method. 问题是什么:代码完全可以正常工作,但是我的paint方法返回了一个错误。

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. 我尝试过的方法:通过将代码块变成在paint方法中调用的方法,隔离引起问题的代码行。

Used Variables: 使用的变量:

lengthCountArray.length (the arrays length dictates how many numbers are along the bottom) lengthCountArray.length(数组的长度指示底部有多少个数字)

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). 引起问题的行是“ int barWidth = canvasWidth / lengthCountArray.length; //这将计算条的距离”(第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! 在导致StackOverflow之前,我已经尝试修复了数小时,因此,如果您需要更多信息,请不要犹豫!

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语句,但我必须抵消除以0的情况。if语句如下:

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! 如果您找到更好的方法,请发表评论或留下答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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