import java.io.*;public class TestCatchExceptions {
public static void main(String[] args){
FileInputStream in=null;
try{
in=new FileInputStream("Cat.java"); int b=in.read();
while(b!=-1)
{System.out.println((char)b);
b=in.read();}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
try{in.close();
}catch(IOException e)
{e.printStackTrace();}}
}
}at java.io.FileInputStream.<init>(Unknown Source)
at TestCatchExceptions.main(TestCatchExceptions.java:7)
Exception in thread "main" java.lang.NullPointerException
at TestCatchExceptions.main(TestCatchExceptions.java:18)谢谢~!
public static void main(String[] args){
FileInputStream in=null;
try{
in=new FileInputStream("Cat.java"); int b=in.read();
while(b!=-1)
{System.out.println((char)b);
b=in.read();}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
try{in.close();
}catch(IOException e)
{e.printStackTrace();}}
}
}at java.io.FileInputStream.<init>(Unknown Source)
at TestCatchExceptions.main(TestCatchExceptions.java:7)
Exception in thread "main" java.lang.NullPointerException
at TestCatchExceptions.main(TestCatchExceptions.java:18)谢谢~!
你用绝对定位试试
抛得异常啊?加个判断
if(in != null)
in.close();
in=new FileInputStream("Cat.java");
// 这句话没有成功打开Cat.java in为null,估计是路径不对finally{
try{in.close();// 这儿就出错了
}catch(IOException e)
{e.printStackTrace();}}
}