线程 "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException 中的异常:100
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100
我们正在尝试制作一个带有方块 (10 x 10) 的游戏板。但是,当 运行 下面的 class 时,我们不断收到 ArrayIndexOutOfBoundsException。由于某些原因,当 运行 在另一台设备上相同 class 时,不会给出此错误。
Board.java
public class Board extends JComponent {
public Board() {
}
public static String[] gameElements = new String[100];
String[][] Map = new String[10][10];
int positionX = 50;
int positionY = 50;
int i = 0;
String currentLevel = "1";
@Override
public void paintComponent(Graphics g) {
loadLevel();
for (int y = 0; y < Map.length; y++) {
for (int x = 0; x < Map.length; x++) {
new Tile(x, y).paintComponent(g);
Map[y][x] = gameElements[i];
g.drawString(Map[y][x], positionY, positionX);
positionY = positionY + 50;
System.out.print("[" + Map[y][x] + "]");
i++;
}
positionY = 50;
positionX = positionX + 50;
System.out.println();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("SleutelBarricade");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent chart = new Board();
frame.add(chart);
frame.setVisible(true);
}
public void readTextFile(String fileName) {
try {
FileReader fileReader = new FileReader(fileName + ".txt");
BufferedReader buffer = new BufferedReader(fileReader);
String splitBy = ",";
String line = buffer.readLine();
for (int i = 0; i < gameElements.length; i++) {
gameElements = line.split(splitBy);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void loadLevel() {
readTextFile(currentLevel);
}
}
部分异常:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100
at Main.GameBoard.Board.paintComponent(Board.java:45)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
您永远不会在双嵌套循环开始之前重置 i
。将变量移入内部或将其重置为 0
.
@Override
public void paintComponent(Graphics g) {
int i = 0;
loadLevel();
for (int y = 0; y < Map.length; y++) {
for (int x = 0; x < Map.length; x++) {
new Tile(x, y).paintComponent(g);
Map[y][x] = gameElements[i];
g.drawString(Map[y][x], positionY, positionX);
positionY = positionY + 50;
System.out.print("[" + Map[y][x] + "]");
i++;
}
positionY = 50;
positionX = positionX + 50;
System.out.println();
}
}
我还建议您重新组织 class 以使其更易于阅读。您不必一字不差地遵循这一点,但请尝试将相似的事物归为一组。这将使事情变得更好,并帮助其他人更轻松地遵循您的计划。
有了这个简单的提示,杂散的 int i = 0
(实例变量)就很容易被发现了。
public class Board extends JComponent {
// I. Static variables
public static String[] gameElements = new String[100];
// II. Instance variables (These should be private or protected)
private String[][] map = new String[10][10];
private int positionX = 50;
private int positionY = 50;
private int i = 0; // <------------------------- Hey, you don't belong here!
private String currentLevel = "1";
// III. Getter/Setters
public String[] getMap() {
return map;
}
public void setMap(String[] map) {
this.map = map;
}
// IV. Constructor
public Board() { }
// V. Overrides
@Override
public void paintComponent(Graphics g) { }
// VI. Custom Instance Methods
public void readTextFile(String fileName) { }
public void loadLevel() { }
// VII. Main Method
public static void main(String[] args) { }
}
我们正在尝试制作一个带有方块 (10 x 10) 的游戏板。但是,当 运行 下面的 class 时,我们不断收到 ArrayIndexOutOfBoundsException。由于某些原因,当 运行 在另一台设备上相同 class 时,不会给出此错误。
Board.java
public class Board extends JComponent {
public Board() {
}
public static String[] gameElements = new String[100];
String[][] Map = new String[10][10];
int positionX = 50;
int positionY = 50;
int i = 0;
String currentLevel = "1";
@Override
public void paintComponent(Graphics g) {
loadLevel();
for (int y = 0; y < Map.length; y++) {
for (int x = 0; x < Map.length; x++) {
new Tile(x, y).paintComponent(g);
Map[y][x] = gameElements[i];
g.drawString(Map[y][x], positionY, positionX);
positionY = positionY + 50;
System.out.print("[" + Map[y][x] + "]");
i++;
}
positionY = 50;
positionX = positionX + 50;
System.out.println();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("SleutelBarricade");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent chart = new Board();
frame.add(chart);
frame.setVisible(true);
}
public void readTextFile(String fileName) {
try {
FileReader fileReader = new FileReader(fileName + ".txt");
BufferedReader buffer = new BufferedReader(fileReader);
String splitBy = ",";
String line = buffer.readLine();
for (int i = 0; i < gameElements.length; i++) {
gameElements = line.split(splitBy);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void loadLevel() {
readTextFile(currentLevel);
}
}
部分异常:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100
at Main.GameBoard.Board.paintComponent(Board.java:45)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
您永远不会在双嵌套循环开始之前重置 i
。将变量移入内部或将其重置为 0
.
@Override
public void paintComponent(Graphics g) {
int i = 0;
loadLevel();
for (int y = 0; y < Map.length; y++) {
for (int x = 0; x < Map.length; x++) {
new Tile(x, y).paintComponent(g);
Map[y][x] = gameElements[i];
g.drawString(Map[y][x], positionY, positionX);
positionY = positionY + 50;
System.out.print("[" + Map[y][x] + "]");
i++;
}
positionY = 50;
positionX = positionX + 50;
System.out.println();
}
}
我还建议您重新组织 class 以使其更易于阅读。您不必一字不差地遵循这一点,但请尝试将相似的事物归为一组。这将使事情变得更好,并帮助其他人更轻松地遵循您的计划。
有了这个简单的提示,杂散的 int i = 0
(实例变量)就很容易被发现了。
public class Board extends JComponent {
// I. Static variables
public static String[] gameElements = new String[100];
// II. Instance variables (These should be private or protected)
private String[][] map = new String[10][10];
private int positionX = 50;
private int positionY = 50;
private int i = 0; // <------------------------- Hey, you don't belong here!
private String currentLevel = "1";
// III. Getter/Setters
public String[] getMap() {
return map;
}
public void setMap(String[] map) {
this.map = map;
}
// IV. Constructor
public Board() { }
// V. Overrides
@Override
public void paintComponent(Graphics g) { }
// VI. Custom Instance Methods
public void readTextFile(String fileName) { }
public void loadLevel() { }
// VII. Main Method
public static void main(String[] args) { }
}