4
    import java.io.*;
    class AccountInfo {

    private String lastName;
    private String firstName;
    private int age;
    private float accountBalance;
    protected AccountInfo(final String last,final String first,final int ag,final float balance) throws IOException{

        lastName=last;
        firstName=first;
        age=ag;
        accountBalance=balance;
    }
    public void saveState(final OutputStream stream){try{

        OutputStreamWriter osw=new OutputStreamWriter(stream);
        BufferedWriter bw=new BufferedWriter(osw);
        bw.write(lastName);
        bw.newLine();
        bw.write(firstName);
        bw.write(age);
        bw.write(Float.toString(accountBalance));
        bw.close();}
        catch(IOException e){
            System.out.println (e);
        }
    } 
    public void restoreState(final InputStream stream)throws IOException{
        try{


            InputStreamReader isr=new InputStreamReader(stream);
            BufferedReader br=new BufferedReader(isr);
            lastName=br.readLine();
            firstName=br.readLine();
            age=Integer.parseInt(br.readLine());
            accountBalance=Float.parseFloat(br.readLine());
            br.close();}
            catch(IOException e){
                System.out.println (e);
        }

    }

}
    class accounto{
        public static void main (String[] args) {try{



            AccountInfo obj=new AccountInfo("chaturvedi","aayush",18,18);
            FileInputStream fis=new FileInputStream("Account.txt");
            FileOutputStream fos=new FileOutputStream("Account,txt");
            obj.saveState(fos);
            obj.restoreState(fis);}
            catch(IOException e){
                System.out.println (e);
        }
    }
}

im getting the following error: Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:454) at java.lang.Integer.parseInt(Integer.java:527) at AccountInfo.restoreState(accounto.java:43) at accounto.main(accounto.java:60)

4

4 回答 4

8

这是你的代码:

BufferedReader br=new BufferedReader(isr);
//...
age=Integer.parseInt(br.readLine());

这是BufferedReader.readLine()(粗体我的)的文档:

包含行内容的字符串,不包括任何行终止字符,或者null是否已到达流的末尾

事实上,你从来没有真正检查是否达到了 EOF。你能确定你的输入吗(结果你不能)。

也适用于Integer.parseInt()

抛出:

NumberFormatException- 如果字符串不包含可解析的整数。

null几乎不是“可解析的整数”。最简单的解决方案是检查您的输入并以某种方式处理错误:

String ageStr = br.readLine();
if(ageStr != null) {
  age = Integer.parseInt(br.readLine())
} else {
  //decide what to do when end of file
}
于 2012-07-22T13:35:56.190 回答
3

从这一行:

Integer.parseInt(br.readLine());

所以看起来你正在阅读流的结尾,所以它br.readLine()是空的。而且您不能将 null 解析为 int。

于 2012-07-22T13:35:23.973 回答
3

br.readLine()方法返回 null,它无法转换为 Integer - 可能的原因是已到达流的末尾。

于 2012-07-22T13:35:35.880 回答
0

1.我认为返回的值br.readLine()null

2.所以它不能从 String 转换为 Integer

3.这就是你得到的原因NumberFormatException

4.为了解决这个问题,将该代码包装成try/catch块。

 try{

        age = Integer.parseInt(br.readLine());


  }catch(NumberFormatException ex){


        System.out.println("Error occured with during conversion");
 }
于 2012-07-22T14:06:08.030 回答