【问题标题】:Java Index Out Of Bounds Exception When Loading Tile Map加载瓦片地图时Java索引越界异常
【发布时间】:2014-06-13 12:50:35
【问题描述】:

在我为帮助我创建新游戏而制作的工具中,我需要能够加载和保存平铺地图并对其进行编辑。当我单击鼠标时,它会将鼠标悬停的当前图块更改为我选择的图块。出于某种原因,每当我单击任何图块时,第一个图块总是会更改为某种奇怪的黑色,并且会引发索引越界异常。它说 “线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 在 Engine.Map.loadUpdatedMap(Map.java:161) 在 Engine.Pane.mousePressed(Pane.java:65)。我检查了文本文件并将其替换为当前的图块大小(当它应该为 0 时)。代码贴在下面,有什么想法吗?

这是发生错误的地方(Map.java)

public void loadUpdatedMap(){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(fileName + ".txt")));

            for (int i = 0; i < mapHeight; i++){
                String read = reader.readLine();
                String[] skips = read.split(" ");
                for (int j = 0; j < mapWidth; j++){
                    map[i][j] = Byte.parseByte(skips[j]);
                }
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

这是我替换瓷砖的方式 (Pane.java)

public void mousePressed(MouseEvent e) {
    if (map.created && map.tileSize != 0){
        currentX = e.getX() / map.tileSize;
        if (e.getY() / map.tileSize > 1){
            currentY = (e.getY() - map.tileSize) / map.tileSize;
        }
        else{
            currentY = (e.getY() / map.tileSize);
        }
        map.setTile(currentX, currentY, currentSelection);
        map.loadUpdatedMap();
    }
}

用于保存地图的代码

public void saveToLocation(){
    try{
        if (fileName == null){
            fileName = "default";
        }
        FileOutputStream fout = new FileOutputStream(new File(System.getProperty("user.dir"), fileName + ".txt"));
        System.out.println("File was saved to: " + new File(System.getProperty("user.dir"), fileName + ".txt"));
        fout.write(String.valueOf(tileSize).getBytes());
        fout.write(System.getProperty("line.separator").getBytes());
        fout.write(String.valueOf(mapWidth).getBytes());
        fout.write(System.getProperty("line.separator").getBytes());
        fout.write(String.valueOf(mapHeight).getBytes());
        fout.write(System.getProperty("line.separator").getBytes());

        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                fout.write(String.valueOf(map[i][j]).getBytes());
                fout.write(String.valueOf(" ").getBytes());
            }
            fout.write(System.getProperty("line.separator").getBytes());
        }
        fout.close();
    }catch (Exception e){
        e.printStackTrace();
    }

}

在点击任何东西之前

第一次点击后

文本文件结果

【问题讨论】:

  • 到目前为止你尝试过什么?您是否逐行调试了代码,以查看值与您预期的不同之处?

标签: java save loading multidimensional-array indexoutofboundsexception


【解决方案1】:

之后你的ships 数组有多大

 String[] skips = read.split(" ");

根据您的代码,它是mapWidth,然后您尝试从数组中提取那么多元素。

也许

for (int j = 0; j < skips.length; j++){

会更正确

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 2013-03-30
    • 2013-03-05
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多