Java:我继续获得奇怪的输出

时间:2017-11-15 08:05:57

标签: java

每当我运行此代码时,我都会继续:

java.lang.NumberFormatException: null

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at UseEmployeeRecord.main(UseEmployeeRecord.java:24)

作为输出。

// The "UseEmployeeRecord" class.
import java.awt.*;
import hsa.Console;
import java.io.*;


public class UseEmployeeRecord {

   public static void main (String[] args) throws IOException {

       BufferedReader fr = new BufferedReader (new FileReade("employees.txt"));
        String name;
        int num;
        double hr, hw;

        EmployeeRecord employee[] = new EmployeeRecord [10];

        for (int i = 0 ; i < employee.length ; i++)
        {
            name= fr.readLine ();
            num = Integer.parseInt (fr.readLine ());
            hr = Double.parseDouble (fr.readLine ());
            hw = Double.parseDouble (fr.readLine ());

            employee [i] = new EmployeeRecord (name, num, hr, hw);

       }
   }
}

1 个答案:

答案 0 :(得分:0)

您尚未粘贴employees.txt的内容

例外情况很明显。

如果employees.txt中的值是字符串表示“整数”,例如

Integer.parseInt("1")不会抛出异常,但肯定Integer.parseInt(null)Integer.parseInt("x")会抛出异常。因此,对于调试,请打印readLine的值,看看它是否真的代表整数。