黑马程序员技术交流社区

标题: 读文件操作中的空指针异常 [打印本页]

作者: 徐帅    时间: 2012-5-31 09:33
标题: 读文件操作中的空指针异常
已经在关闭资源的时候作出了空指针的判断,为何在读文件产生错误的时候
除了 File read error !还有 nullpointerException ?请帮忙看看,谢谢了啊

import java.io.*;

class FileReaderDemo2
{
        public static void main(String[] args)
        {
                FileReader fr = null;
                char buf[] = new char[1024];
                int num = 0;

                try
                {
                        fr = new FileReader("love.txt");
                        while ((num = fr.read(buf))!= -1)
                        {
                           System.out.println(new String(buf,0,num));
                        }
                       
                }

                catch (IOException e)
                {
                        System.out.println("File read error !");
                }

                finally
                {
                        try
                        {
                                if(fr!= null);
                                fr.close();
                        }
                        catch (IOException e)
                        {
                                System.out.println("File close error !");
                        }
                       
                }
               
        }
}



作者: 刘伯阳    时间: 2012-5-31 09:45
刚调试了下你的代码错误在这里:
1、在你的文件所在目录下没有love.txt文件,所以找不到文件,找不到文件怎么会读呢?所以会报出File read error !
2、在你的Finally里面,try
                        {
                                if(fr!= null);               //注意——你的if后面加了个分号 这样的意思就不是如果fr不为空则关闭fr了。
                                fr.close();
                        }

详细代码应该改为这样:
import java.io.*;

class FileReaderDemo2
{
        public static void main(String[] args)
        {
                FileReader fr = null;
                char buf[] = new char[1024];
                int num = 0;

                try
                {
                        fr = new FileReader("love.txt");
                        while ((num = fr.read(buf))!= -1)
                        {
                           System.out.println(new String(buf,0,num));
                        }
                        
                }

                catch (IOException e)
                {
                        System.out.println("File read error !");
                }

                finally
                {
                       
                        try
                        {
                                if(fr!= null)
                                fr.close();
                        }
                        catch (IOException e)
                        {
                                System.out.println("File close error !");
                        }
                        
                }
               
        }
}

作者: 黑马—陈磊    时间: 2012-5-31 09:45
你的代码我运行过了,没问题,是不是 fr = new FileReader("G:\\c.txt");中的路径错了呢?你自己检查一下哈,我估计是路径错了,报空指针异常
作者: 黑马—陈磊    时间: 2012-5-31 09:45
你的代码我运行过了,没问题,是不是 fr = new FileReader("G:\\c.txt");中的路径错了呢?你自己检查一下哈,我估计是路径错了,报空指针异常
作者: 张文强    时间: 2012-5-31 09:56
运行了下就时文件路径不正确或者不存在造成的
作者: 凡延海    时间: 2012-5-31 10:39
楼主的判断语句  if(fr!= null);  有了分号就会任务if语句后的语句块为空,这个判断功能就不起作用了,后面的fr.close()语句不论fr为不为空都会执。因为没有找到文件fr引用就是空引用,因而就会产生NullPointerException这样的异常。楼主修改一下代码就可以了
  1.   finally
  2.                 {
  3.                         
  4.                         try
  5.                         {
  6.                                 if(fr!= null)
  7.                                 fr.close();
  8.                         }
  9.                         catch (IOException e)
  10.                         {
  11.                                 System.out.println("File close error !");
  12.                         }
  13.                         
  14.                 }
复制代码
楼主也可以把异常修改一下,将错就错的修改,呵呵。
  1. finally
  2.                {
  3.                        try
  4.                        {
  5.                                if(fr!= null);
  6.                                fr.close();
  7.                        }
  8.                        catch (Exception e)
  9.                        {
  10.                                System.out.println("File close error !");
  11.                        }
  12.                        
  13.                }
复制代码
希望对楼主有帮助
作者: 徐帅    时间: 2012-5-31 12:02
刘伯阳 发表于 2012-5-31 09:45
刚调试了下你的代码错误在这里:
1、在你的文件所在目录下没有love.txt文件,所以找不到文件,找不到文件怎 ...

是自己的粗心,谢谢纠错
作者: 徐帅    时间: 2012-5-31 12:03
heima_dinghua 发表于 2012-5-31 10:00
原因在于 if(fr!= null);语句加了分号,导致fr.close();语句无论在什么情况下都会执行,而不是根据if判断的 ...

是自己粗心,谢谢提醒啊
作者: 徐帅    时间: 2012-5-31 12:03
凡延海 发表于 2012-5-31 10:39
楼主的判断语句  if(fr!= null);  有了分号就会任务if语句后的语句块为空,这个判断功能就不起作用了,后面 ...

解答的很详细。很是感谢!!!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2