I need help about java.lang.IndexOutOfBoundsException - ArrayList

3.8k views Asked by At

I'm doing a project about Guitar Hero clone. When I was running my game (just concept) an error occurred: java.lang.IndexOutOfBoundsException.

package tapnstrum2;

import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.swing.*;

public class String1 extends JFrame implements ActionListener {
Image S1;
GNode G;
PlayIG P;
int m =0;

public String1(){
    ImageIcon S1i = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01.png");
    S1 = S1i.getImage();
}
public Image getImage(){
    return S1;
}

public void keyPressed(KeyEvent e) throws FileNotFoundException, InterruptedException{
    ArrayList GNodes = P.getGNodes();
    GNode GN = (GNode) GNodes.get(m);
    int key = e.getKeyCode();
    ImageIcon S1iExact = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01_Exact.png");
    ImageIcon S1iMissed = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01_Missed.png");
    if (key == KeyEvent.VK_F1){
        for (int i=0; i< GNodes.size(); i++){
        if (GN.getY()+47>=380 && GN.getY()+47<=420)
            S1 = S1iExact.getImage();
        else
            S1 = S1iMissed.getImage();
        }
    }
    m++;
if (m>=GN.size())
 m=0;
}
public void keyReleased(KeyEvent e){
        int key = e.getKeyCode();
        ImageIcon S1i = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01.png");
        if (key == KeyEvent.VK_F1){
           S1 = S1i.getImage();
        }
}

@Override
public void actionPerformed(ActionEvent ae) {
    repaint();
}

}

I tried to get bounds of images form an ArrayList that has been read from a simple text file contains just 0 or 1 each line (1 is drawImage and 0 is not to) and check the bounds. This error has Index is may equal to how many time I pressed F1. Although i tried to put a condition for m in last lines.

Update my error message:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at tapnstrum2.String1.keyPressed(String1.java:28)
    at tapnstrum2.PlayIG$AL.keyPressed(PlayIG.java:98)
    at java.awt.Component.processKeyEvent(Component.java:6483)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
    at java.awt.Component.processEvent(Component.java:6302)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
    at java.awt.Component.dispatchEventImpl(Component.java:4752)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    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)

You can download my project here if there is anything useful https://mega.co.nz/#F!ql80jDKY!fM91p7phB7LfhTs75rSK-Q

3

There are 3 answers

0
dat_digital On BEST ANSWER

i found my problem. each time when i remove a element from an ArrayList, the elements behind it pushed up automatically. in my program, each time a node hit the bottom of the frame, it disappear and removed from that ArrayList. so the index I need to check is just 0, the first element of the ArrayList. Now I can fix. Thank you all so much for helping me find out. :)

public void keyPressed(KeyEvent e) throws FileNotFoundException, InterruptedException{
    ArrayList GNodes = PlayIG.getGNodes();
    GNode GN = (GNode) GNodes.get(0);
    System.out.println("" + m + " " + GNodes);
    int key = e.getKeyCode();
    ImageIcon S1iExact = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01_Exact.png");
    ImageIcon S1iMissed = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01_Missed.png");
    if (key == KeyEvent.VK_F1){
            if (GN.getY()+47>=370 && GN.getY()+47<=420)
                S1 = S1iExact.getImage();
            else
                S1 = S1iMissed.getImage();

    }
12
codehitman On

The problem is on this line:

GNode GN = (GNode) GNodes.get(m);

If the index m is > GNodes.size() then it will throw that exception. You have two options:

  1. Add that statement in a try/catch block.
  2. Add an if statement:

    GNode GN = null;
    if (m > 0 && m < GNodes.size()) {
        GN = (GNode) GNodes.get(m);
    }
    
0
Sam On

You need to check that m is less than the size of the GNodes ArrayList in this line:

GNode GN = (GNode) GNodes.get(m); //NetBeans inform this line contain an error

The error indicates that m is outside the bounds of the ArrayList, which just means that it's either equal to or greater than the length of the ArrayList, or it's less than 0.

For example,

When m is 3 and your ArrayList is [x, y, z]

GNodes.get(m) attempts to get the value after z, which doesn't exist and causes Java to throw the exception.