java - Getting runtime error for unknown reason; referencing ObjectInputStream -


i'm trying make program stores array of class i've denoted entry. class stores user's input based on command, name, number, , note. after entry received sent file, through objectinput/objectoutput, , stores later use.

sort of simple contact list can periodically called , updated time time.

i'm running sort of weird error(seen below). if me fix error 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() {         (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");         }         (int = 0; i<length; i++) {             if (entrylist[i].name.comparetoignorecase(entry.name) <0) {                 //?             }         }     }      public void dofind(string term) {         // 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 entered 1 8 characters");     system.out.println("use \"e\" enter, \"f\" find, \"l\" list, , \"q\" quit");     system.out.println();      {     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 found inbetween * above):

exception in thread "main" java.io.eofexception     @ java.io.objectinputstream$peekinputstream.readfully(unknown source)     @ java.io.objectinputstream$blockdatainputstream.readshort(unknown source)     @ java.io.objectinputstream.readstreamheader(unknown source)     @ java.io.objectinputstream.<init>(unknown source)     @ phonebook.doload(phonebook.java:39)     @ phonebook.main(phonebook.java:57) 

in comments confirm contacts.txt empty. reason eofexception thrown. objectoutputstream , objectinputstream use specific protocol , format includes serialization streamheader objectinputstream expects present when constructed. file empty can't read header , eofexception thrown when attempts read beyond end of file.

you cannot use empty file when using objectinputstream. if need work empty file, should not using objectoutputstream , objectinputstream, write own text (or binary format) files. eg bufferedwriter , bufferedreader and/or scanner.

alternatively, need prepared handle eofexception in doload method , return empty array or null.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -