【问题标题】:How to identify if ObjectInputStream has available object?如何识别 ObjectInputStream 是否有可用对象?
【发布时间】:2012-06-05 06:14:14
【问题描述】:

在尝试识别序列化对象是否可用并使用套接字接收它时出现错误NullPointerException。如何识别ObjectInputStream 是否有可用对象? 首先,我尝试读取文本,然后尝试从可能不存在的同一个套接字 Lot 对象()中读取。

        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();
                    }
    }

【问题讨论】:

    标签: java sockets serialization


    【解决方案1】:
    if((Lot)ois.readObject() != null)
    

    这部分本身从 Socket 读取对象。因此,您在代码中从 Socket 读取对象的 3 倍。如果您只有一个或多个对象进入套接字,您可以读取该对象并捕获异常!。 如下所示

     //..loop start
             try {
                    lot = (Lot)ois.readObject(); 
                 }
             catch (Exception e) 
               { 
    //      do some handling, skip the object! put a continue: or something
    
                }
        //do what ever you want to do with `lot`
    
        //..loop end
    

    现在,根据您的代码,您尚未初始化您的 ObjectInputStream 对象。

    做一个ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

    如果您在这里省略了代码,那是我的错误,否则请也初始化套接字!

    【讨论】:

    • 代码已更新。我尝试在 Thread 构造函数中初始化。在构造函数中使用 try catch 是一个好习惯吗?
    • 只需在 try-catch 中读取 Object。捕获一般异常,因为不应按照编码标准捕获 NUllPointerException。我认为这应该可行。
    • 在构造函数中使用 try-catch 没有错误,但不要只为 stackTrace 留下 catch 块,这很糟糕 :)。在那里做一些好的处理动作,放一些日志。或会使异常“很好”的东西:D
    • 我可以这样比较:if(ois.readObject() instanceof Lot)
    【解决方案2】:

    根据其他答案,包括已删除的答案,您调用 readObject() 两次并将第一个结果丢弃。您应该重新组织您的代码,以便它可以阻止readObject()

    您还有其他问题。您正在为null? 测试readObject() 的结果,但如果您在发件人处写了null,它只会返回null。我怀疑您将其用作 EOS 测试,但它是无效的。 readObject() 向 EOS 抛出 EOFException。您应该重新组织您的代码,以便它可以阻止readObject()

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 2011-11-29
      • 2011-10-10
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多