Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whats wrong?(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);
        }
    }
}

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)

like image 569
Aayush Chaturvedi Avatar asked Dec 09 '22 22:12

Aayush Chaturvedi


2 Answers

This is your code:

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

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

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

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

Also for Integer.parseInt():

Throws:

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

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
}
like image 109
Tomasz Nurkiewicz Avatar answered Dec 11 '22 10:12

Tomasz Nurkiewicz


From this line:

Integer.parseInt(br.readLine());

So it looks like you're reading the end of the stream so br.readLine() is null. And you can't parse null into an int.

like image 30
Jon Lin Avatar answered Dec 11 '22 10:12

Jon Lin