线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:100

时间:2016-04-07 11:46:15

标签: java indexoutofboundsexception paintcomponent tiles

我们正在尝试用瓷砖(10 x 10)创建一个游戏板。但是,在运行下面的类时,我们不断获得ArrayIndexOutOfBoundsException。出于某种原因,在另一台设备上运行相同的类时,不会给出此错误。

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)

1 个答案:

答案 0 :(得分:0)

在双嵌套循环开始之前,您永远不会重置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();
    }
}

我还建议您重新组织课程,以便更容易阅读。你不必遵循这个字母,但尝试将类似的东西分组。这将使事情更加突出,并帮助其他人更轻松地遵循您的计划。

通过这个简单的提示,很容易发现迷路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) { }
}