java无效的IndexOutOfBoundException

时间:2018-11-21 04:54:31

标签: java arrays indexoutofboundsexception

我不知道为什么,但是在启动程序时偶尔出现以下错误消息。在大多数情况下,它运行良好,但是每次启动时不时出现此错误。这没有任何意义,因为如您所见,它正在尝试引用index 1 of a size 3 array,这应该不是问题。该数组是一个ArrayList,我在内部和外部循环中都使用嵌套的for循环,而不是每个嵌套的,而是普通的for(int i =0; i < size; i++)样式。不知道我做错了什么还是遇到某种错误。我将在下面的代码部分中报告错误。

此特定实例发生在这里:

int x = fontArray.get(i).getPaintLetters().get(i2)[0]*Animator.pixMulti;

但是我也看到了该部分的其他数组调用中也发生了这种情况。它仅在第一次调用时发生。它以循环方式运行,并且对于该程序实例不再发生。预先感谢。

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 3
        at java.util.ArrayList.rangeCheck(ArrayList.java:653)
        at java.util.ArrayList.get(ArrayList.java:429)
        at Engine.Board.paintFont(Board.java:461)
        at Engine.Board.paintComponent(Board.java:332)
        at javax.swing.JComponent.paint(JComponent.java:1053)
        at javax.swing.JComponent.paintToOffscreen(JComponent.java:5223)
        at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1572)
        at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1495)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1265)
        at javax.swing.JComponent._paintImmediately(JComponent.java:5171)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4982)
        at javax.swing.RepaintManager$4.run(RepaintManager.java:824)
        at javax.swing.RepaintManager$4.run(RepaintManager.java:807)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782)
        at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731)
        at javax.swing.RepaintManager.access$1300(RepaintManager.java:64)
        at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
        at java.awt.EventQueue.access$400(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:697)
        at java.awt.EventQueue$3.run(EventQueue.java:691)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这是报告错误的代码块。

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    paintFont(g);


    Toolkit.getDefaultToolkit().sync();
}

private void paintFont(Graphics g){
    Graphics2D g2d = (Graphics2D) g;

    for(int i = 0; i < fontArray.size(); i++){
        Image img = fontArray.get(i).getImage();
        for(int i2 = 0; i2 < fontArray.get(i).getPaintLetters().size(); i2++){
            int x = fontArray.get(i).getPaintLetters().get(i2)[0]*Animator.pixMulti;
            int y = fontArray.get(i).getPaintLetters().get(i2)[1]*Animator.pixMulti;
            int xPaint = fontArray.get(i).getLetWpaint();
            int yPaint = fontArray.get(i).getLetHpaint();
            int imgX = fontArray.get(i).getPaintLetters().get(i2)[2];
            int imgY = fontArray.get(i).getPaintLetters().get(i2)[3];
            int imgXW = fontArray.get(i).getLetW();
            int imgYH = fontArray.get(i).getLetH();
            g2d.drawImage(img,x,y,x+xPaint,y+yPaint,imgX,imgY,imgX+imgXW,imgY+imgYH,this);
        }
    }
}

这是getPaintLetters返回的代码部分。 paintLetters是返回的内容。

ArrayList<int[]> paintLetters = new ArrayList<int[]>();

private void setArray(){
paintLetters.clear();

Point point;
int fontX = x, fontY = y;
int currentChar = 0;
for(int i = 0; i < strArray.size(); i++){
    while(currentChar < strArray.get(i).length()){
        int lets[] = new int[4];
        point = setLet(strArray.get(i).charAt(currentChar));
        lets[0] = fontX;
        lets[1] = fontY;
        lets[2] = point.x;
        lets[3] = point.y;
        paintLetters.add(lets);
        fontX += 7;
        currentChar++;
    }
    currentChar = 0;
    fontX = x;
    fontY += 8;
}
strArray.clear();
}

public ArrayList<int[]> getPaintLetters() {
        return paintLetters;
}

0 个答案:

没有答案