Java 在while循环中,从文件读取时的BufferedReader输入始终为空

Java 在while循环中,从文件读取时的BufferedReader输入始终为空,java,arrays,file-io,bufferedreader,Java,Arrays,File Io,Bufferedreader,我正在尝试读取一个文本文件并创建一个数组。使用第一行中显示的文件的前2个值设置数组。然后将文件中的所有剩余项添加到数组中。由于某种原因,我的数组也总是空的。要使其正常运行,需要进行哪些更正 public static Grid createGrid(String filename) throws IOException { BufferedReader input; String inputLine; char[][] grid;

我正在尝试读取一个文本文件并创建一个数组。使用第一行中显示的文件的前2个值设置数组。然后将文件中的所有剩余项添加到数组中。由于某种原因,我的数组也总是空的。要使其正常运行,需要进行哪些更正

public static Grid createGrid(String filename) throws IOException {
        BufferedReader input;
        String inputLine;
        char[][] grid;
        Grid currentGrid;
        int currentLine = 0;
        try{
            input = new BufferedReader(new FileReader(filename));
            inputLine = input.readLine();
            String[] tokens = inputLine.split(" ");
            if(tokens.length != 2){
                throw new IllegalArgumentException();
            }
            int height = Integer.parseInt(tokens[0]);
            int width = Integer.parseInt(tokens[1]);
            grid = new char[height][width];
            while(inputLine != null){ // Giving me (Condition 'inputLine != null' is always 'true') in ide
                System.out.println(inputLine);
                inputLine = input.readLine();
                for(int i = 0; i < inputLine.length() - 1; i++){
                    char currentGem = inputLine.charAt(i);
                    grid[currentLine][i] = currentGem;
                }
                currentLine++;
            }
            input.close();
            currentGrid = new Grid(grid);
        }
        catch(IOException e){
            System.out.println(e.getMessage());
            currentGrid = null;
        }
        return currentGrid; // Giving me (Value 'currentGrid' is always 'null') in ide
    }
公共静态网格createGrid(字符串文件名)引发IOException{
缓冲读取器输入;
字符串输入线;
char[][]网格;
电网电流;
int currentLine=0;
试一试{
输入=新的BufferedReader(新的文件读取器(文件名));
inputLine=input.readLine();
String[]tokens=inputLine.split(“”);
if(tokens.length!=2){
抛出新的IllegalArgumentException();
}
int height=Integer.parseInt(标记[0]);
int width=Integer.parseInt(标记[1]);
网格=新字符[高度][宽度];
而ide中的(inputLine!=null){//Giving me(条件'inputLine!=null'总是'true')
系统输出打印LN(输入线);
inputLine=input.readLine();
对于(int i=0;i
在您的代码中

while(inputLine!=null)

。。。将始终为
true
,因为
inputLine
是您的
字符串
,并且在运行此检查时它将始终具有值。原因是,一旦您将
null
读入
inputLine
(当您到达文件末尾时),而不是运行循环检查,您将在尝试运行下一行代码时抛出
NullPointerException
<代码>用于(int i=0;i。这反过来会导致
currentGrid
始终为
null
,因为此时它仍然声明为
null

要解决这个问题,只需添加一个
inputLine=input.readLine()while
之前添加code>,然后将
while
中的同一行移动到
while
的末尾,如下所示

public static Grid createGrid(String filename) throws IOException {
    Grid currentGrid = null;
    try {
        BufferedReader input = new BufferedReader(new FileReader(filename));
        String inputLine = input.readLine();
        String[] tokens = inputLine.split(" ");
        if(tokens.length != 2){
            throw new IllegalArgumentException();
        }
        int height = Integer.parseInt(tokens[0]);
        int width = Integer.parseInt(tokens[1]);
        char[][] grid = new char[height][width];
        int currentLine = 0;
        inputLine = input.readLine(); // added before while
        while(inputLine != null) {
            System.out.println(inputLine);
            for(int i = 0; i < inputLine.length() - 1; i++){
                char currentGem = inputLine.charAt(i);
                grid[currentLine][i] = currentGem;
            }
            currentLine++;
            inputLine = input.readLine(); // last line in while
        }
        input.close();
        currentGrid = new Grid(grid);
    }
    catch(IOException e) {
        System.out.println(e.getMessage());
        currentGrid = null;
    }
    return currentGrid; 
}
公共静态网格createGrid(字符串文件名)引发IOException{
Grid currentGrid=null;
试一试{
BufferedReader输入=新BufferedReader(新文件读取器(文件名));
字符串inputLine=input.readLine();
String[]tokens=inputLine.split(“”);
if(tokens.length!=2){
抛出新的IllegalArgumentException();
}
int height=Integer.parseInt(标记[0]);
int width=Integer.parseInt(标记[1]);
字符[][]网格=新字符[高度][宽度];
int currentLine=0;
inputLine=input.readLine();//在while之前添加
while(inputLine!=null){
系统输出打印LN(输入线);
对于(int i=0;i

哦,你也会注意到我把你的声明搬来搬去了。您没有在
try{…}catch
块之外声明
BufferedReader input
String inputLine
char[][]网格
。另外,您只需要声明
int currentLine=0就在您需要它之前(这只是一个很好的实践)

如果“BufferedReader input…always null”标题会误导您,
inputLine.split(“”
如何不引发异常?@CarlosHeuberger感谢您指出out将纠正它,并且您在文件末尾没有得到任何
NullPointerException
??大多数情况下,在任何
catch
块中都有一个(附加的)
e.printStackTrace()
——这就是为什么您没有看到异常,只是打印消息可能会让人困惑(这里的异常原因是
readLine
的返回在处理之前没有检查null)@CarlosHeuberger在我的代码中没有发现任何其他错误,除了上面提到的两个点。当然,正如我已经写过的:“在处理之前,readLine的返回不会被检查为null”,见下文-异常应该是问题的一部分,是解决大多数问题的重要来源……起初我是这么认为的,但
catch(IOException ex)
不会由NPE触发。。。。这一定是在别的地方发生的。提示:在((inputLine=input.readLine())!=null)时,可以按
的方式进行读取{
避免重复这修复了我在这方面遇到的错误,这些错误不再出现在我的ide中,但仍然得到nullPointerException,如前所述。@ic0n将nullPointerException添加到捕获中,并查看引发异常的行号。@ic0n,你说得对吗……你的问题得到了回答吗?如果是的话请勾选勾号.Tx,将此标记为已解决。