NoSuchElementException发生在代码内

问题描述 投票:-1回答:1

这里,我使用了两个类来组合序列化和反序列化以及集合的概念。运行程序时,出现运行时异常“ NoSuchElementException”。

我已经使用Employee类存储Employee的基本信息。

DemoEmployee类是声明四个方法的主类。

  1. 添加员工的信息(它是我创建的链接列表对象的对象)。
  2. 显示链接列表对象中存在的所有Employee类对象的信息。
  3. 序列化链接列表对象
  4. 要反序列化链接列表对象

    如何解决此问题?


import java.io.Serializable;

public class Employee_6033 implements Serializable {
    private int empid_6033;
    private String empname_6033;
    private String empdes_6033;
    private int empsalary_6033;

    public Employee_6033(int empid_6033, String empname_6033, String empdes_6033, int empsalary_6033) {
        // TODO Auto-generated constructor stub
        this.empid_6033 = empid_6033;
        this.empname_6033 = empname_6033;
        this.empdes_6033 = empdes_6033;
        this.empsalary_6033 = empsalary_6033;
    }

    int getEmpId_6033() {
        return empid_6033;
    }

    String getEmpName_6033() {
        return empname_6033;
    }

    String getEmpDes_6033() {
        return empdes_6033;
    }

    int getEmpSalary_6033() {
        return empsalary_6033;
    }
}

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class DemoEmployee_6033 {
    static List<Employee_6033> l_6033 = new LinkedList<Employee_6033>();
    int n_6033;
    int i_3033;

    void addEmployee_6033() {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter the number of employees : ");
        n_6033 = input.nextInt();

        for (i_3033 = 1; i_3033 <= n_6033; i_3033++) {
            System.out.println("\nEnter Employee " + i_3033 + "'s Details :");
            System.out.print("\tEnter ID : ");
            int id_6033 = input.nextInt();
            input.nextLine();
            System.out.print("\tEnter Name : ");
            String name_6033 = input.nextLine();
            System.out.print("\tEnter Designation : ");
            String des_6033 = input.nextLine();
            System.out.print("\tEnter Salary : ");
            int salary_6033 = input.nextInt();

            l_6033.add(new Employee_6033(id_6033, name_6033, des_6033, salary_6033));
        }
        input.close();
    }

    void display_6033() {
        int j_6033 = 1;

        for (Employee_6033 ref_6033 : l_6033) {
            System.out.println("Employee " + j_6033);
            System.out.println("\tID : " + ref_6033.getEmpId_6033());
            System.out.println("\tName : " + ref_6033.getEmpName_6033());
            System.out.println("\tDesignation : " + ref_6033.getEmpDes_6033());
            System.out.println("\tSalary : " + ref_6033.getEmpSalary_6033());
            j_6033++;
        }
    }

    void serialize_6033() {
        try {
            String filename_6033 = "";
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the name of the file you want to Read from : ");
            filename_6033 = input.nextLine();

            // Saving of object in a file
            FileOutputStream file_6033 = new FileOutputStream(filename_6033);
            ObjectOutputStream out_6033 = new ObjectOutputStream(file_6033);

            // Method for serialization of object
            out_6033.writeObject(l_6033);

            out_6033.close();
            file_6033.close();

            System.out.println("Object has been serialized");
            input.close();
        }

        catch (IOException ex) {
            System.out.println("IOException is caught");
        }
    }

    void deserialize_6033() {
        l_6033 = null;
        try {
            String filename_6033;
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the name of the file you want to Write to : ");
            filename_6033 = input.nextLine();
            // Reading the object from a file
            FileInputStream file_6033 = new FileInputStream(filename_6033);
            ObjectInputStream in_6033 = new ObjectInputStream(file_6033);

            // Method for deserialization of object
            l_6033 = (List<Employee_6033>) in_6033.readObject();

            in_6033.close();
            file_6033.close();

            System.out.println("Object has been deserialized ");
            display_6033();
            input.close();
        }

        catch (IOException ex) {
            System.out.println("IOException is caught");
        }

        catch (ClassNotFoundException ex) {
            System.out.println("ClassNotFoundException is caught");
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        DemoEmployee_6033 de_6033 = new DemoEmployee_6033();

        int choice_6033;
        do {
            System.out.println("\n********* MENU *********");
            System.out.println("--------------------------");
            System.out.println("1. Add Employee");
            System.out.println("2. Display Employee Details");
            System.out.println("3. Write Data into a File - Serialization");
            System.out.println("4. Read Data from a File - Deserialization");
            System.out.println("5. Exit");
            System.out.println("--------------------------");
            System.out.print("Enter you Choice : ");
            choice_6033 = input.nextInt(); //Error comes here after 1 iteration.
            switch (choice_6033) {
            case 1:
                de_6033.addEmployee_6033();
                break;
            case 2:
                de_6033.display_6033();
                break;
            case 3:
                de_6033.serialize_6033();
                break;
            case 4:
                de_6033.deserialize_6033();
                break;
            case 5:
                break;
            default:
                System.out.println("You have entered an invalid choice!");
                break;
            }
        } while (choice_6033 != 5);
        input.close();
    }

}
java exception nosuchelementexception
1个回答
0
投票

NoSuchElementException被抛出,因为您广泛地调用了input.close(),在这种情况下,它只能被调用一次。我已经删除了所有出现的内容(main方法除外),并且您的代码工作正常。

出于这个原因,实际上,调用Scanner.close()会关闭整个System.in,并且每个以后相关的方法调用(例如您的情况下的input.nextInt()都会导致异常。

© www.soinside.com 2019 - 2024. All rights reserved.