1

我编写了以下代码来从文本文件中读取瓦片图,但是由于我无法弄清楚的原因,我收到了一个空点异常错误。

读者代码:

BufferedReader br = new BufferedReader(new FileReader(s));

        readMapWidth = Integer.parseInt(br.readLine());
        readMapHeight = Integer.parseInt(br.readLine());
        map = new int[readMapHeight][readMapWidth];

        for(int row = 0; row < readMapHeight; row++) {
            String line = br.readLine();
            System.out.println(line);
            String[] tileValues = line.split(",");
            for(int col = 0; col < readMapWidth; col++){
                map[row][col] = Integer.parseInt(tileValues[col]);  

            }
        }
    }

文本文件内容:

在此处输入图像描述

命令控制台作为错误返回的内容:

java.lang.NullPointerException
at TileMap.<init>(TileMap.java:58)

这是行:

                String[] tileValues = line.split(",");

线条读得很好,我可以将地图绘制到屏幕上。但是我需要一个实际存储的二维数组来稍后用于寻路,但是这条线返回一个空值,我不明白为什么。

以下是“System.out.println(line)”返回的内容让我感到困惑:

[final line of the map here, map lines print as normal]
null 

空?我不明白,我的文本文件只有 27 行关于 split 的空异常来自哪里?

4

3 回答 3

1

你应该在开始之前替换你的数据文件

var readed_data:String = read whole data as String;

while (readed_data.search("\r") != -1) //MAC line reset cmd
{
     readed_data = readed_data.replace("\r", ""); //replace to nullstring
{

while (readed_data.search("\r") != -1) //UNIX line reset cmd
{
     readed_data = readed_data.replace("\n", ""); //replace to nullstring
{

//And so Windows uses both of "\n" and "\r" strings in text files as a quarantee, now you can read your data as you wish... now all data is single line data and you can split with "," comma after this time easily and without unneeded characters
于 2014-01-20T10:45:01.660 回答
1

您似乎已到达文件末尾或遇到空行,导致您的行 String 在这里为空:

        String[] tileValues = line.split(",");

最好先检查文件结尾,或者至少在对文件进行拆分之前先在线检查空值。

  for(int row = 0; row < readMapHeight; row++) {
            String line = br.readLine();
            if(line == null || line.isEmpty()) {
                System.out.println("Line is empty or null");
            } else {
                System.out.println(line);
                String[] tileValues = line.split(",");
                for(int col = 0; col < readMapWidth; col++){
                   map[row][col] = Integer.parseInt(tileValues[col]);  

            }
         }
        }
于 2013-08-12T00:41:32.740 回答
1

有两点很突出...

  1. br.readLine()null到达文件末尾时返回。
  2. 您的文件仅包含 25 行数据(在高度和宽度行之后)。IE27 - 2 = 25

更好的方法可能是做类似的事情......

BufferedReader br = null;

try {
    br = new BufferedReader(new FileReader(s));

    readMapWidth = Integer.parseInt(br.readLine());
    readMapHeight = Integer.parseInt(br.readLine());
    map = new int[readMapHeight][readMapWidth];

    int row = 0;
    String text = null;
    while ((text = br.readline()) != null) {
        System.out.println(line);
        String[] tileValues = line.split(",");
        for(int col = 0; col < readMapWidth; col++){
            map[row][col] = Integer.parseInt(tileValues[col]);  
        }
        row++;
    }

    // Zero index rows...
    if (row < readMapHeight - 1) {
        throw IOException("Expected title height does not match actual row height");
    }    
} finally {
    try {
        br.close();
    } cacth (Exception exp) {
    }
}

如果您的纹理大小正确,您可能期望总共有 27 行,而不仅仅是 27 行的纹理,因此您需要相应地调整行数

于 2013-08-12T00:41:56.420 回答