如何修复此堆栈溢出错误实例?

How Do I Fix This Instance of Stack Overflow Error?

我打算用静态方法创建一个 class,需要执行以下操作:

  1. return 连接员工 ID 号和员工对象的映射,如文本文件中所述
  2. 有一个 toString 方法,它 return 是有关具有输入 ID 号的员工的信息

我相信我几乎完成了这项任务,但是,我最近遇到的问题是 WhosebugError 的一个相当极端的案例,我似乎无法找到解决方案。相关资料如下:

首先,这是class提供的不可编辑的主class我打算写一个class以符合:

 public static void main(String[] args)  //this is line 6 for reference
  {
    Map<String, Employee> data = Employee.load(); 
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter ID of employee you're looking for");
    String id = keyboard.nextLine();
    System.out.println(data.get(id));
  }

这是我写的 class 应该几乎可以按预期工作:

public Employee(String b) { //this is line 6 for reference
        Map<String, Employee> m=load();   //line 7
        if (m.containsKey(b)){
            String value=m.get(b).toString();
            System.out.println(b+" -> "+value);
        }
    }

    public static Map<String,Employee> load() {
        File f=new File("employees.txt");
        Map<String,Employee> result=new LinkedHashMap<>();
        String ID = null;
        String name = null;
        try {
            BufferedReader in = new BufferedReader(new FileReader(f)); //line 20
            String line;
            String[] values;
            int i=0;
            while(((line=in.readLine())!=null)){
                if (i>0){ //this skips the first line with irrelevant informaion
                line=in.readLine();
                values= line.split(" ", 2);
                ID=values[0];
                if (values.length>1){
                name= values[1].replaceAll("\s{2,}"," ").trim();}}
                i++;}
        } catch (IOException e) {
            e.printStackTrace();
        }
        Employee e=  new Employee(name); //line 35
        result.put(ID, e);
        return result;
    }

一个应该发生的例子是程序应该这样工作:

输出:输入您要查找的员工的 ID

输入:39-7290036

输出:39-7290036 -> Elroy Bevis ebevish@hc360.com

但是程序甚至在输入请求的初始输出之前就崩溃了。 出现的错误如下:

Exception in thread "main" java.lang.WhosebugError
    at java.base/java.nio.ByteBuffer.limit(ByteBuffer.java:265)
    at java.base/java.nio.Buffer.<init>(Buffer.java:223)
    at java.base/java.nio.ByteBuffer.<init>(ByteBuffer.java:284)
    at java.base/java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:63)
    at java.base/java.nio.ByteBuffer.allocate(ByteBuffer.java:351)
    at java.base/sun.nio.cs.StreamDecoder.<init>(StreamDecoder.java:264)
    at java.base/sun.nio.cs.StreamDecoder.<init>(StreamDecoder.java:244)
    at java.base/sun.nio.cs.StreamDecoder.forInputStreamReader(StreamDecoder.java:79)
    at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:74)
    at java.base/java.io.FileReader.<init>(FileReader.java:75)
    at Employee.load(Employee.java:20)
    at Employee.<init>(Employee.java:7) //these last two lines repeat for several upon several more lines
    at Employee.load(Employee.java:35) 

您收到 Whosebug 异常,因为 load() 方法创建了一个新的 Employee 对象,并且在 Employee load() 的构造函数中被回调等等

Employee 的构造函数应该类似于

public Employee(String id, String name) {
    this.id = id
    this.name = name
}

并且您需要在 load 的 while 循环内创建一个新的 Employee,因此将其添加到 while 循环的末尾,就在 i++ 行之前

Employee employee = new Employee(ID, name);
result.put(ID, employee);

并删除while循环后的相应行