Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这里会出现NullPointerException_Java_Nullpointerexception - Fatal编程技术网

Java 为什么这里会出现NullPointerException

Java 为什么这里会出现NullPointerException,java,nullpointerexception,Java,Nullpointerexception,我正在读一个代表迷宫模式的文本文件。 每一行被读入一个一维字符数组,然后一个一维数组被插入到二维字符数组中 在下面的方法中,在 line = (input.readLine()).toCharArray(); private void fillArrayFromFile()引发IOException { BufferedReader输入=新的BufferedReader(新文件读取器(“maze.txt”); //读取文本文件的前两行并确定二维数组的x和y长度 mazarra

我正在读一个代表迷宫模式的文本文件。 每一行被读入一个一维字符数组,然后一个一维数组被插入到二维字符数组中

在下面的方法中,在

        line = (input.readLine()).toCharArray();

private void fillArrayFromFile()引发IOException
{
BufferedReader输入=新的BufferedReader(新文件读取器(“maze.txt”);
//读取文本文件的前两行并确定二维数组的x和y长度
mazarray=newchar[Integer.parseInt(input.readLine())][Integer.parseInt(input.readLine())];
char[]行=新字符[10];
//将每行输入添加到Mazarray
对于(int x=0;x
BufferedReader.readLine()
在没有更多的输入可读取时返回
null
。有关详细信息,请参阅。

BufferedReader.readLine()
在没有更多要读取的输入时返回
null
。有关详细信息,请参见。

显而易见的答案是,
input.readLine()
返回
null
,并且由于您调用的对象的方法不指向任何对象,因此会得到
NullPointerException

但是,这个问题的根源在于您对行和列的感知与对文本文件的感知之间的差异。另外,如果我正确地阅读了您的代码,那么您正在循环错误的索引

下面是一个示例文本文件:

4
6
a b c d
b c d a
a d c a
b d b a
c d a a
b a b a
考虑这一点的一个好方法是考虑您拥有的行和列的数量

例如,上面的文本文件显示“我有4列6行”

这也意味着
x
范围从
0
3
y
范围从
0
5

在您的术语中,x-length是列数,y-length是行数

当然,请记住,假设索引在增加,则行跨,列向下

这是非常重要的。因为x和y本质上是网格中特定位置的索引

虽然这可能是原因,但您也有一些语义错误,我将在下面修复这些错误

BufferedReader input = new BufferedReader(new FileReader("maze.txt"));

//Read the first two lines of the text file and determine the x and y length of the 2d array
int mazeWidth = Integer.parseInt(input.readLine());  //A.K.A number of columns
int mazeHeight= Integer.parseInt(input.readLine());  //A.K.A number of rows

//          ranges: [0...3][0...5] in our example
mazeArray = new char[mazeWidth][mazeHeight];

char [] line;

//Add each line of input to mazeArray
for (int y = 0; y < mazeHeight; y++) 
{
    line = (input.readLine()).toCharArray();

    if ( line.length != mazeWidth )
    {
        System.err.println("Error for line " + y + ". Has incorrect number of characters (" + line.length + " should be " + mazeWidth + ").");
    }
    else {
        System.out.print(y + " : ");
        System.out.println(java.util.Arrays.toString(line));
        mazeArray[y] = line;
    }
}
BufferedReader输入=新的BufferedReader(新文件阅读器(“maze.txt”);
//读取文本文件的前两行并确定二维数组的x和y长度
int mazeWidth=Integer.parseInt(input.readLine())//A.K.A.许多列
int-mazeHeight=Integer.parseInt(input.readLine())//又称为多行
//范围:[0…3][0…5]在我们的示例中
mazarray=新字符[mazeWidth][mazehheight];
字符[]行;
//将每行输入添加到Mazarray
对于(int y=0;y
显而易见的答案是,
input.readLine()
返回
null
,并且由于您调用的是一个不指向任何对象的方法,因此会得到一个
NullPointerException

但是,这个问题的根源在于您对行和列的感知与对文本文件的感知之间的差异。另外,如果我正确地阅读了您的代码,那么您正在循环错误的索引

下面是一个示例文本文件:

4
6
a b c d
b c d a
a d c a
b d b a
c d a a
b a b a
考虑这一点的一个好方法是考虑您拥有的行和列的数量

例如,上面的文本文件显示“我有4列6行”

这也意味着
x
范围从
0
3
y
范围从
0
5

在您的术语中,x-length是列数,y-length是行数

当然,请记住,假设索引在增加,则行跨,列向下

这是非常重要的。因为x和y本质上是网格中特定位置的索引

虽然这可能是原因,但您也有一些语义错误,我将在下面修复这些错误

BufferedReader input = new BufferedReader(new FileReader("maze.txt"));

//Read the first two lines of the text file and determine the x and y length of the 2d array
int mazeWidth = Integer.parseInt(input.readLine());  //A.K.A number of columns
int mazeHeight= Integer.parseInt(input.readLine());  //A.K.A number of rows

//          ranges: [0...3][0...5] in our example
mazeArray = new char[mazeWidth][mazeHeight];

char [] line;

//Add each line of input to mazeArray
for (int y = 0; y < mazeHeight; y++) 
{
    line = (input.readLine()).toCharArray();

    if ( line.length != mazeWidth )
    {
        System.err.println("Error for line " + y + ". Has incorrect number of characters (" + line.length + " should be " + mazeWidth + ").");
    }
    else {
        System.out.print(y + " : ");
        System.out.println(java.util.Arrays.toString(line));
        mazeArray[y] = line;
    }
}
BufferedReader输入=新的BufferedReader(新文件阅读器(“maze.txt”);
//读取文本文件的前两行并确定二维数组的x和y长度
int mazeWidth=Integer.parseInt(input.readLine())//A.K.A.许多列
int-mazeHeight=Integer.parseInt(input.readLine())//又称为多行
//范围:[0…3][0…5]在我们的示例中
mazarray=新字符[mazeWidth][mazehheight];
字符[]行;
//将每行输入添加到Mazarray
对于(int y=0;y
因为您在文件末尾?因为您在文件末尾?所以基本上您应该提取下一行,并在将其转换为字符数组之前确定其是否为null:
String lineStr=(input.readLine());如果(lineStr==null)中断;line=lineStr.toCharArray()
Thank you@twall and fivedigitSo基本上,您应该获取下一行,并在将其转换为字符数组之前确定其是否为null:
String lineStr=(input.readLine());如果(lineStr==null)bre