-2

i have Drugbean object which has to be inserted to data base i used object input stream in order get the object from another class when i insert the data of the object into data base i get java.io.EOFException

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int flag=0;
        ObjectInputStream inputStream = null;
        ObjectOutputStream outputStream = null;
        DrugModel drugBean = new DrugModel();
        List<DrugModel> itemFromClient = new ArrayList<DrugModel>();
        List<String> itemToClient = new ArrayList<String>();    
        try {           
            inputStream = new ObjectInputStream(request.getInputStream());          
            while((drugBean = (DrugModel) inputStream.readObject())!=null){             
                flag = MakeConnection.insertDrugRecord(drugBean);                           
            }                           
            inputStream.close();                        
            if (flag == 1) {
                itemToClient.add("true");
                outputStream = new ObjectOutputStream(response.getOutputStream());
                outputStream.writeObject(itemToClient);
            } else {
                itemToClient.add("false");
                outputStream = new ObjectOutputStream(response.getOutputStream());
                outputStream.writeObject(itemToClient);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

cosole.error

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 com.medeil_plus.SyncDrugTable.doPost(SyncDrugTable.java:47)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Manik
  • 11
  • 4
  • could you post complete exception stacktrace ? – Ravi Jan 26 '18 at 06:07
  • pls see the error above @Ravi – Manik Jan 26 '18 at 06:12
  • Are you expecting the input stream to contain multiple objects? What is sending the requests? Are you sure the sending program correctly sets up the corresponding `ObjectOutputStream` and writes the expected objects to it? Without seeing the sending code we cannot possibly help you. – Jim Garrison Jan 26 '18 at 06:22
  • @JimGarrison I am most surprised to find that that is a blatant misquotation. What it *actually* says is 'Any attempt to read object data which exceeds the boundaries of the *custom* data written by the corresponding *`writeObject`* method'. My emphasis. There is no 'custom data' in evidence here (which comes from custom `writeObject()` methods inside the `Serializable` object). There is nothing there about `readObject()` returning null at EOS either. See the duplicate, and there are many others. If you don't believe it, please try it before you debate this further with me. – user207421 Jan 26 '18 at 06:56
  • I'm not debating, just really curious why `EOFException` is not mentioned for `readObject()` at all. Since that is the way EOF is communicated, you'd think it would be mentioned explicitly. – Jim Garrison Jan 26 '18 at 07:08

1 Answers1

0

EOFException when calling readObject() is normal. It happens when you reach end of stream.

Your mistake is in assuming it returns null at end of stream. It doesn't. It can return a null any time you wrote a null.

So your read loop should be while (true), with an explicit catch for EOFException to exit it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Just for my clarification, are you saying making mentioned change will resolve the issue ? even `readObject` doesn't have anything ? – Ravi Jan 26 '18 at 06:27
  • Of course I'm saying that. I posted this as an answer. I don't know what you mean by '`readObject()` doesn't have anything'. – user207421 Jan 26 '18 at 06:28
  • If I understand correctly, you meant loop might not have executed because `inputStream.readObject()` might have null ? – Ravi Jan 26 '18 at 06:31
  • That is *exactly the opposite* of what it says in my answer. I can only suggest you read it again. I'm not going to just repeat myself here. – user207421 Jan 26 '18 at 06:32