NullPointerException异常而读文件到数组 [英] NullPointerException while reading file into array

查看:182
本文介绍了NullPointerException异常而读文件到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在读入文件到一个数组已经越来越空指针异常。我意识到,当它为空和别的东西需要出现异常。该阵列雷区已经初始化。唯一的例外发生在雷区[I] [J] = input.charAt(J)+;

I've been getting null pointer exception while reading a file into an array. i realized that the exception appears when it is null and something else is required. The array minefield has already been initialized. The exception happens on " minefield[i][j]=input.charAt(j)+"";"

我想在这种格式的文件阅读:

I'm trying to read in a file with this format:

#of row
#of column
abcd
efgh
ijkl

这是code:

     try {
            BufferedReader in =new BufferedReader (new FileReader(name+".txt"));
            String input=in.readLine();      
            row = Integer.parseInt(input);
            input=in.readLine();
            col = Integer.parseInt(input);
            int c =0;
            input=in.readLine();
            for (int i=0;i<row;i++){
            input=in.readLine();
            for (int j=0;j<col;j++){
                  System.out.println (input.charAt(j));
                  minefield[i][j]=input.charAt(j)+"";
               }
            }
            System.out.println("The file has been loaded");
            in.close();
         }
            catch(IOException iox){
               System.out.println ("Error reading file");
            }

您帮助是极大的AP preciated。
编辑:对不起,我留下的东西了。

Your help is greatly appreciated. Sorry i left something out.

推荐答案

方法的readLine()返回null。

Method readLine() returns null when the end of the stream is reached, but you don't check it.

请参阅的http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--

这篇关于NullPointerException异常而读文件到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆