java - How to identify if ObjectInputStream has available object? -
getting error nullpointerexception while trying identify if serialized object available , receive using socket. how identify if objectinputstream has available object? firs off try read text try read same socket lot object ()which may not there.
public class threadin extends thread{ bufferedreader in; printstream outconsole; socket socket; objectinputstream ois; string str; lot lot; threadin(bufferedreader input, printstream inoutput, socket s){ str = ""; in= input; socket= s; try { ois = new objectinputstream(socket.getinputstream()); } catch (ioexception e) { e.printstacktrace(); } outconsole = inoutput; } public void run() { while(!exit_thread){ try { if(in.ready()){ try { str= in.readline(); thread.sleep(100); } catch (ioexception e) { exit_thread= true; break; } catch (interruptedexception e) { e.printstacktrace(); } outconsole.println("received:"+str); } } catch (ioexception e) { e.printstacktrace(); } try { if((lot)ois.readobject() != null){ lot = (lot)ois.readobject(); if (lot!=null){outconsole.println(lot.tostring());} outconsole.println((string)ois.readobject()); } } catch (exception e) { e.printstacktrace(); } }
if((lot)ois.readobject() != null) this part reads object socket., reading 3 times object socket in code. if have 1 object coming in socket, or more, can read object , catch exception!. below
//..loop start try { lot = (lot)ois.readobject(); } catch (exception e) { // handling, skip object! put continue: or } //do ever want `lot` //..loop end and now, per code, have not initialized objectinputstream object.
do objectinputstream ois = new objectinputstream(socket.getinputstream());
if omitted code here, mistake, else pls initialize socket also!
Comments
Post a Comment