2015-06-13 73 views
2

我正在做一個關於吉他英雄克隆的項目。當我運行我的遊戲時(只是概念)發生錯誤:java.lang.IndexOutOfBoundsException我需要關於java.lang.IndexOutOfBoundsException的幫助 - ArrayList

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(); 
} 

}

我試圖讓圖像的邊界形成已經從一個簡單的文本文件中讀取一個ArrayList只包含0或1的每一行(1個drawImage,0是不是),並檢查邊界。 這個錯誤有索引可能等於我按了多少次F1。雖然我試圖在最後一行中加上m的條件。

更新我的錯誤信息:

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) 

你可以在這裏下載我的項目,如果有什麼有用 https://mega.co.nz/#F!ql80jDKY!fM91p7phB7LfhTs75rSK-Q

回答

0

我發現我的問題。每次當我從ArrayList中移除一個元素時,它後面的元素自動推上去。在我的程序中,每次節點碰到幀底時,它都會從該ArrayList中消失並移除。所以我需要檢查的索引只是0,ArrayList的第一個元素。現在我可以修復。非常感謝你幫助我找出答案。 :)

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(); 

    } 
1

的問題是在這條線:

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

如果指數m> GNodes.size()那麼它會拋出這個異常。你有兩個選擇:

  1. 加入這一聲明在try/catch塊。
  2. 添加一個if語句:

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

@tom沒錯。堅持嘗試/抓住。 – codehitman

+0

@tom喜歡加入下限? – codehitman

+1

@tom啊明白了!索引從0開始,對嗎?所以它應該是n-1。 – codehitman

1

您需要檢查m是小於GNodes的ArrayList在這條線的大小:

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

的錯誤指示是m超出了ArrayList的範圍,這意味着它等於或大於ArrayList的長度,或者它小於0.

例如,

當m 3,你的ArrayList[x, y, z]

GNodes.get(m)嘗試z後得到的值,它不存在,並導致Java來拋出異常。

+0

謝謝你的:) –