【问题标题】:Java ObjectInputStream not being instantiatedJava ObjectInputStream 没有被实例化
【发布时间】:2016-07-27 19:58:04
【问题描述】:

我正在创建一种信使程序,客户端与服务器等进行通信。 我偶然发现的问题是在尝试创建 ObjectInputStream 和 ObjectOutputStream 时。下面是实例化对象流的方法:

private void initializeStreams() {
        try {
            input = new ObjectInputStream(socket.getInputStream());
            if (input != null) {
                System.out.println("ObjectInputStream successfully initiated");
            } else {
                System.out.println("ObjectInputStream is null but did not return an exception when being instantiated");
            }
        } catch (IOException ioe) {
            System.out.println("Could not initialize ObjectInputStream: " + ioe.getMessage());
        }
        try {
            output = new ObjectOutputStream(socket.getOutputStream());
            if (output != null) {
                System.out.println("ObjectOutputStream successfully initiated");
            } else {
                System.out.println("ObjectOutputStream is null but did not return an exception when being instantiated");
            }
        } catch (IOException ioe) {
            System.out.println("Could not initialize ObjectOutputStream: " + ioe.getMessage());
        }
    }`

这个方法的问题是没有调用 System.out.println() 方法,尽管至少据我所知,应该调用每个流的一个。例如,在实例化 ObjectInputStream 时,它应该要么抛出异常(它显然不会因为 System.out.println() 没有被调用),返回 null(这似乎也不是这种情况,因为 System .out.println() 没有被调用)或成功创建 ObjectInputStream 对象,因为没有调用 System.out.println() 对象。为什么它没有遇到任何这些情况?我是否错过了可能发生的另一种情况?

附:是的,initializeStreams() 方法是从程序中调用的,我刚刚检查了它,将 System.out.println() 放在方法的第一行。

谢谢

【问题讨论】:

  • 添加断点并调试。
  • 尝试调试您的代码,看看当您一次单步执行该方法时会发生什么。
  • 感谢您的宝贵时间,我设法解决了问题

标签: java exception-handling objectinputstream objectoutputstream


【解决方案1】:

尝试在 finally-cluster 的控制台上写一些东西。 可能是抛出了一个没有被捕获的异常。

但你会看到...不是吗。

我的第一个提示:调试您的程序,这通常对我有帮助。

不过你也可以试试这个:

private void initializeStreams() {
        input = null;
        output = null;
        try {
            input = new ObjectInputStream(socket.getInputStream());
            }
        } catch (IOException ioe) {
            System.out.println("Could not initialize ObjectInputStream: " + ioe.getMessage());
        }

        //just copied the if outside of the try-catch-cluster
        if (input != null) {
                    System.out.println("ObjectInputStream successfully initiated");
        } else {
          System.out.println("ObjectInputStream is null but did not return an exception when being instantiated");


        try {
            output = new ObjectOutputStream(socket.getOutputStream());

        } catch (IOException ioe) {
            System.out.println("Could not initialize ObjectOutputStream: " + ioe.getMessage());
        }

        if (output != null) {
            System.out.println("ObjectOutputStream successfully initiated");
        } else {
            System.out.println("ObjectOutputStream is null but did not return an exception when being instantiated");
                }
    }`

这是我会尝试的一件事,以找出问题所在或问题所在。 即使它没有那么大的意义;)

【讨论】:

  • 非常感谢您的时间和帮助,我最终能够解决这个问题。我在另一条评论中解释了如何,如果您有兴趣可以去看看
【解决方案2】:

好吧,你只能抓到IOException。例如,您的代码中可能有 RuntimeException。在这种情况下,你不会到达你的 catch 块。

IOException改成Exception就知道原因了。

【讨论】:

  • 感谢您的建议,我最终解决了这个问题
【解决方案3】:

new ObjectInputStream() 可以抛出 IOException 以外的异常,但您的 try-catch 只捕获 IOException。如果抛出的异常是其他类型之一怎么办?

【讨论】:

  • 感谢您的建议,我最终能够解决这个问题,这与调用 socket.getInputStream() 时程序卡住有关,因为我的服务器上发生了一个奇怪的问题,它是无法实例化其输出流,因此 socket.getInputStream() 方法阻塞了程序,因为它找不到来自另一端的输出流
【解决方案4】:

IOException 替换为Exception 类,它是所有异常类的父类。无论是什么异常,它肯定会在catch 块中使用Exception 类捕获。

所以在你的代码中的任何地方都用catch (Exception ioe)替换catch (IOException ioe)。然后你可能会发现异常来自哪里。

【讨论】:

  • 感谢您的建议,我最终能够解决此问题。如果您有兴趣,我在另一条评论中写了如何
猜你喜欢
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 2023-04-09
  • 2013-09-06
  • 2015-05-20
  • 2016-08-25
  • 2016-02-15
  • 1970-01-01
相关资源
最近更新 更多