我正在做一个关于吉他英雄克隆的项目。当我运行我的游戏(只是概念)时发生错误: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();
}
}
我尝试从一个简单的文本文件读取的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 :(得分:1)
问题在于这一行:
ValueError: time data '05/29/15' does not match format 'mm/dd/YY'
如果索引GNode GN = (GNode) GNodes.get(m);
为m
,那么它将抛出该异常。您有两种选择:
> GNodes.size()
/ try
块中添加该语句。添加if语句:
catch
答案 1 :(得分: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抛出异常。
答案 2 :(得分:0)
我发现了我的问题。每次当我从ArrayList中删除一个元素时,它后面的元素会自动向上推。在我的程序中,每当一个节点到达框架的底部时,它就会从该ArrayList中消失并删除。所以我需要检查的索引只是0,即ArrayList的第一个元素。现在我可以解决了。非常感谢你帮助我找到答案。 :)
is_admin