Java奇怪意外的IndexOutOfBounds

时间:2016-03-30 10:36:27

标签: java indexoutofboundsexception

我在一个大项目中有以下功能:

private void evalLinear(float w1, float I, int axis) {
    try{
        for (i = 0; i < numPoints; i++) {

                wv[axis][i] = wrr1[axis] - W * x[i] / L;
                wm[axis][i] = (float) (wmm1[axis] + wrr1[axis] * x[i] - 0.5 * W * x[i] * x[i] / L);
                wy[axis][i] = (float) (1. / (6. * E * I) * (wrr1[axis]* Math.pow(x[i], 3.) - 3. * -wmm1[axis] * x[i] * x[i] - W / 4. * Math.pow(x[i], 4) / L));
                wd[axis][i] = (float) (1. / (6. * E * I) * (wrr1[axis] * 3 * Math.pow(x[i], 2.) - 6. * -wmm1[axis] * x[i] - W / 4. * 4 * Math.pow(x[i], 3.) / L));
        }
    }catch (IndexOutOfBoundsException e) {
        p.println("numPoints="+numPoints);
        p.println("i="+i+", IndexOutOfBoundsException: " + e.getMessage());
    }
}

它运行两次并抛出以下错误:

numPoints=7
i=2IndexOutOfBoundsException: 7
numPoints=7
i=7IndexOutOfBoundsException: 7

首先我不明白当IndexOutOfBounds i=2 numPoints=7时我如何获得i<numPoints,然后我无法理解{{1}可以取值7

修改

这是完整的StackTrace

axis:0, wy.length:2
i:7, wy[axis].length:7
axis:0, wy.length:2
i:7, wy[axis].length:7
numPoints=7
i=7, IndexOutOfBoundsException: 7
java.lang.ArrayIndexOutOfBoundsException: 7
    at BeamCalc.Element.evalLinear(Element.java:441)
    at BeamCalc.Element.evalWeight(Element.java:345)
    at BeamCalc.LoadInterface$5.actionPerformed(LoadInterface.java:137)
    at javax.swing.JTextField.fireActionPerformed(JTextField.java:492)
    at javax.swing.JTextField.postActionEvent(JTextField.java:705)
    at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:820)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1639)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2851)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2886)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2814)
    at java.awt.Component.processEvent(Component.java:6066)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4652)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4482)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676)
    at java.awt.Component.dispatchEventImpl(Component.java:4524)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4482)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:617)
    at java.awt.EventQueue$2.run(EventQueue.java:615)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)

1 个答案:

答案 0 :(得分:2)

好的......所以让我们解决吧。

wv[axis][i] = wrr1[axis] - W * x[i] / L;
wm[axis][i] = (float) (wmm1[axis] + wrr1[axis] * x[i] - 
    0.5 * W * x[i] * x[i] / L);
wy[axis][i] = (float) (1. / (6. * E * I) * 
    (wrr1[axis]* Math.pow(x[i], 3.) - 3. * 
     -wmm1[axis] * x[i] * x[i] - W / 4. * 
     Math.pow(x[i], 4) / L));
wd[axis][i] = (float) (1. / (6. * E * I) * 
    (wrr1[axis] * 3 * Math.pow(x[i], 2.) - 6. * 
     -wmm1[axis] * x[i] - W / 4. * 4 * Math.pow(x[i], 3.) / L));

第一个语句中没有例外意味着wv[axis][i]wrr1[axis]x[i]都可以。

第二个语句中没有例外意味着wm[axis][i]wmm1[axis]也没关系。

第三个陈述中的例外意味着问题出在LHS上;即wy[axis][i]有错。

这意味着axis >= wy.lengthi >= wy[axis].length

现在你需要弄清楚哪个是问题,以及它为什么会发生。

<强>更新

但接下来:你说你从catch块得到了这个输出:

numPoints=7
i=7IndexOutOfBoundsException: 7

看似不可能。循环中会抛出IndexOutOfBoundsException异常(您说),但i不小于numPoints。并且您不会使用循环更改inumPoints

您是在向我们展示真实的代码,还是已经“消毒”?

更新2

我明白为什么我们会感到困惑。 您在catch块中打印的i不是循环变量。此时循环变量超出范围。你必须打印一个封闭类的字段,或者其他东西!实际上,甚至更糟!循环根本不是声明变量i所以我们有一个循环变量,它是封闭类的一个字段,并且可能与其他方法共享和/或其他线程。 NASTY!

numPoints也不是本地变量,因此 也可以更改。