- import java.io.*;
- class MyLineReader
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr=new FileReader("d:\\xiaonan.txt");
- MiLineNumReader mr=new MiLineNumReader(fr);
- String s=null;
- while ((s=mr.myGetLineNum())!=null)
- {
- System.out.println(s);
- }
- fr.close();
- }
- }
- class MiLineNumReader extends Reader
- {
- private FileReader r;
- int num=0;
-
- MiLineNumReader(FileReader r)
- {
- super(r);
- this.r=r;
- }
- BufferedReader br=new BufferedReader(r);//在这一句抛出了空指针异常
- public String myGetLineNum() throws IOException
- {
- if(br.readLine()!=null)
- {
- return (num++)+br.readLine();
- }
- return null;
- }
- public void close()throws IOException
- {
- r.close();
- }
- public int read(char[] cbuf, int off, int len) throws IOException
- {
- return read(cbuf, off, len);
- }
- }
复制代码 我的问题是,MiLineNumReader mr=new MiLineNumReader(fr);这一句我往构造函数里传递了参数,为什么却没有赋值成功,导致我
BufferedReader br=new BufferedReader(r);这句的r为null,抛出了空指针异常,求解
|