怎么回事?(NumberFormatException:null) [英] whats wrong?(NumberFormatException: null)

查看:142
本文介绍了怎么回事?(NumberFormatException:null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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);
        }
    }
}

我收到以下错误:线程main中的异常java.lang.NumberFormatException:null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527 )AccountInfo.restoreState(accounto.java:43)
accounto.main(accounto.java:60)

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)

推荐答案

这是你的代码:

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

以下是 BufferedReader.readLine() (粗体):

And here is the documentation of BufferedReader.readLine() (bold mine):


包含该行内容的字符串,不包括任何行终止字符 null 如果已到达流的末尾

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

实际上,你永远不会真正检查是否达到了EOF。你能否确定你的输入(事实证明你不能)。

In fact, you never really check whether EOF was reached. Can you be that certain about your input (turns out you can't).

同样适用于 Integer.parseInt()

Also for Integer.parseInt():


抛出:

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

NumberFormatException - if the string does not contain a parsable integer.

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

null is hardly a "parsable integer". The simplest solution is to check your input and handle errors somehow:

String ageStr = br.readLine();
if(ageStr != null) {
  age = Integer.parseInt(br.readLine())
} else {
  //decide what to do when end of file
}

这篇关于怎么回事?(NumberFormatException:null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆