Karan Trehan
Karan Trehan

Reputation: 23

NoSuchElementException ocurring inside the code

Here , I have used two classes to combine the concept of Serialization & Deserialization and Collections. When I run the program , i get a runtime exception "NoSuchElementException".

I have used the Employee class to store the basic info of an Employee.

The class DemoEmployee is the main class in which four methods are declared.

  1. To add an employee's info (it's object to the Linked List object I created).

  2. To display the info of all the Employee class objects present in the Linked List object.

  3. To serialize the Linked List object

  4. To deserialize the Linked List object

    How to resolve this issue?


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

}

Upvotes: 1

Views: 79

Answers (1)

Piotr Niewinski
Piotr Niewinski

Reputation: 1347

NoSuchElementException is thrown, because you call input.close() extensively, where in your case it should be called only once. I have removed input.close(); occurrences (except main method) and your code worked fine.

There reason for this that calling Scanner.close() will in fact close whole System.in and every future related method calls, like input.nextInt() in your case, will result in Exception.

Upvotes: 1

Related Questions