After EOFException variable=null

259 views Asked by At

If I end the client session a EOFException is thrown but I read that this would be normal so I just start a new thread with the same functionality but the value of Restaurant=null; although i wrote it in a .txt file

public void run(){

    try {
        ois= new ObjectInputStream(clientside.getInputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        oos = new ObjectOutputStream(clientside.getOutputStream());
    } catch (IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }

    handlerequest(ois,oos);// exit();
}

I dont think that you need the code of my request handler so I wont attach it to reduce code spamming. The method which invokes the following is the requesthandler

String tempRestaurant=null;  
try {
    fr = new FileReader("Restaurant.txt");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    fr.read(cbuf);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
tempRestaurant=String.valueOf(cbuf);
System.out.println(tempRestaurant);
try {
    oos.writeObject(tempRestaurant);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    try {
    fr.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}    

I´m looking foward to your help pls write if you need more information code etc. :) This is the Exception below which is thrown after exiting the client

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at prealphaserverpackage.clientsidethreads.setRestaurant(Serverpart.java:164)
    at prealphaserverpackage.clientsidethreads.handlerequest(Serverpart.java:205)
    at prealphaserverpackage.clientsidethreads.run(Serverpart.java:96)
Exception in thread "Thread-3" java.lang.NullPointerException
    at java.io.PrintWriter.write(Unknown Source)
    at prealphaserverpackage.clientsidethreads.setRestaurant(Serverpart.java:177)
    at prealphaserverpackage.clientsidethreads.handlerequest(Serverpart.java:205)
    at prealphaserverpackage.clientsidethreads.run(Serverpart.java:96)

i added know your code to mine but still is the restaurant null after EOFException has been thrown...

3

There are 3 answers

0
Sebastian Walla On BEST ANSWER

Fixed it

i forgot to add the break line in the switch case szenario in my request handler sry it was my fault... thx for helping me to avoid the EOFException :)

1
Aubin On

Suggestion:

public void run(){
   try(
      ObjectInputStream  ois =
         new ObjectInputStream ( clientside.getInputStream ());
      ObjectOutputStream oos =
         new ObjectOutputStream( clientside.getOutputStream())) {
      handleRequest( ois, oos );
   }
   catch( IOException e ) {
      e.printStackTrace();
   }
}

void handleRequest( ObjectInputStream ois, ObjectOutputStream oos ) {
   try( BufferedReader fr =
           new BufferedReader( new FileReader( "Restaurant.txt" ))) {
      String tempRestaurant = fr.readLine();
      System.out.println( tempRestaurant );
   }
   catch (IOException e) {
      e.printStackTrace();
   }
}

try-with-resource close the Closeable declared in try( )

1
Stephen C On

You can (and often should) put multiple statements inside a try block if they have common exception handling requirements; e.g.

try {
    ois = new ObjectInputStream(clientside.getInputStream());
    oos = new ObjectOutputStream(clientside.getOutputStream());
    handlerequest(ois,oos);
} catch (IOException ex) {
    ex.printStackTrace();
}

But the flip-side is that when you handle an exception, you need to make sure that the exception handler does the right thing. Printing a stack trace and then continuing as if nothing happened is rarely the correct thing to do. For instance, in your original run() method, if there is an IOException opening the streams, you code will still attempt to call handlerequest ... passing it ois and/or oos instances that are (probably) null. That is NOT the right thing to do!

Finally, if you are using Java 7, you can catch multiple exceptions in a single handler; e.g.

String tempRestaurant=null;  
try {
    fr = new FileReader("Restaurant.txt");
    fr.read(cbuf);
    tempRestaurant = String.valueOf(cbuf);
    System.out.println(tempRestaurant)
    someOtherMethod(); ; 
    oos.writeObject(tempRestaurant);
    fr.close();
} catch (IOException, SomeOtherException ex) {
    ex.printStackTrace();
}

and you can use "try with resources" to automatically close them when you are done: e.g.

String tempRestaurant=null;  
try (FileReader fr = new FileReader("Restaurant.txt")) {
    fr.read(cbuf);
    tempRestaurant = String.valueOf(cbuf);
    System.out.println(tempRestaurant);
} catch (IOException ex) {
    ex.printStackTrace();
}