Tdorno
Tdorno

Reputation: 1571

Getting runtime error for unknown reason; referencing ObjectInputStream

I'm trying to make a program that stores an array of a class I've denoted as entry. This class stores a the user's input based on command, name, number, and a note. After the entry is received it is to be sent to a file, through the ObjectInput/ObjectOutput, and stores for later use.

Sort of like a simple contact list that can be periodically called and updated from time to time.

I'm running into a sort of weird error(seen below). If anyone could help me fix this error I would greatly appreciate it.

Code:

    import java.io.*;
    import java.util.Scanner;

    public class phonebook {

    protected Entry[] entryList = new Entry[200];

    protected int length = 0;

    public void doList() {
        for (int i=0; i<length; i++) {
            System.out.print(entryList[i]);
        }
    }

    public void doAddEntry(Entry entry) throws Exception {
        if (length == 200) {
            throw new Exception("I'm full");
        }
        for (int i = 0; i<length; i++) {
            if (entryList[i].name.compareToIgnoreCase(entry.name) <0) {
                //?
            }
        }
    }

    public void doFind(String term) {
        // look for input in entryList's name fields
    }

    public void doSave() throws Exception {
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("contacts.txt"));
        os.writeObject(entryList);
        os.close();
    }


    public void doLoad() throws Exception {
       ***ObjectInputStream oin = new ObjectInputStream(new FileInputStream("contacts.txt"));***
        entryList = (Entry[])oin.readObject();
        oin.close();
    }

    public static void main(String[] args) throws Exception {
    String line;
    String[] command;
    Scanner input;
    String delimeter;
    delimeter = " ";
    phonebook pbook;
    String cmd;
    String term;

    pbook = new phonebook();
    cmd= "";
    input=new Scanner (System.in);
    **pbook.doLoad();**
    System.out.println("Codes are entered as 1 to 8 characters");
    System.out.println("Use \"e\" for enter, \"f\" for find, \"l\" to list, and \"q\" to quit");
    System.out.println();

    do {
    System.out.print("Command: ");
    line = input.nextLine();
    command = line.split(delimeter);
    if (command[0].equalsIgnoreCase("e")) {
        Entry e = new Entry();
        e.name = command[0];
        System.out.print("Enter Number: ");
        e.number=input.nextLine();
        System.out.print("Enter Notes:  ");
        e.notes=input.nextLine();
        System.out.println("");
            pbook.doAddEntry(e);
        } else if (command[0].equalsIgnoreCase("f")) {
            term=command[0];
            pbook.doFind(term);
            } else if (command[0].equalsIgnoreCase("l")) {
                pbook.doList(); 
                } else if (command[0].equalsIgnoreCase("q")) {
                System.out.println("Exiting Program...");
                pbook.doSave();
                break;
                    } else { 
                        System.out.println("Invalid Command");
                        continue;
                    } 
    } while (true);
    }
    }

Class:

 public class Entry implements java.io.Serializable {
    public String name;
    public String number;
    public String notes;    
} 

Compiler Error Message (code referencing error can be found inbetween * above):

Exception in thread "main" java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at phonebook.doLoad(phonebook.java:39)
    at phonebook.main(phonebook.java:57)

Upvotes: 0

Views: 283

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 109010

In the comments you confirm that contacts.txt is empty. That is the reason that EOFException is thrown. The ObjectOutputStream and ObjectInputStream use a very specific protocol and format that includes a serialization streamheader that the ObjectInputStream expects to be present when constructed. As the file is empty it can't read that header and an EOFException is thrown when it attempts to read beyond the end of the file.

You simply cannot use an empty file when using ObjectInputStream. So if you need it to work with an empty file, then you should not be using ObjectOutputStream and ObjectInputStream, but write your own text (or binary format) files. Eg with a BufferedWriter and a BufferedReader and/or Scanner.

Alternatively, you need to be prepared to handle this EOFException in your doLoad method and return an empty array or null.

Upvotes: 1

Related Questions