一个奇怪的runtimeException
import java.io.*;
public class TestTransForm1 {
public static void main(String[] args){
FileReader fr = null;
BufferedReader br = new BufferedReader(fr);
try {
fr = new FileReader( "d:/java/1.txt ");
} catch (
FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = null;
try {
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
fr.close();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
报错:Exception in thread "main "
java.lang.NullPointerException at java.io.Reader. <init> (Unknown Source)
at java.io.BufferedReader. <init> (Unknown Source)
at java.io.BufferedReader. <init> (Unknown Source)
at TestTransForm1.main(TestTransForm1.java:5)
文件有内容,而且同样的程序我有时候执行就没问题,请问这种情况问题是出在哪里了?
------解决方案--------------------FileReader fr = null;
BufferedReader br = new BufferedReader(fr);
fr是null,现在又把它当作流来包装,它应该还没给内存啊
------解决方案--------------------FileReader fr = null;
BufferedReader br = new BufferedReader(fr);
try {
fr = new FileReader( "d:/java/1.txt ");
} catch (File
NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这段这样改
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader( "d:/java/1.txt ");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
br = new BufferedReader(fr);
这样试试
------解决方案--------------------FileReader fr = new FileReader( "d:/java/1.txt ");;
BufferedReader br = new BufferedReader(fr);