0

我正在創建一種messenger程序,其中客戶端與服務器進行通信等。 我偶然發現的問題是嘗試創建ObjectInputStream和ObjectOutputStream。下面是實例化對象流的方法:未實例化Java ObjectInputStream

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時,它應該拋出一個Exception(它顯然不是因爲System.out.println()沒有被調用),返回null(這也似乎不是這樣,因爲系統.out.println()沒有被調用),或者成功創建了ObjectInputStream對象,它並不是因爲System.out.println()沒有被調用。爲什麼它不會遇到這些情況?我是否錯過了可能發生的另一種情況?

P.S.是的,initializeStreams()方法是從程序中調用的,我只是在方法的第一行檢查了一下System.out.println()。

謝謝

+0

添加斷點和調試。 – Mena

+0

嘗試調試您的代碼,查看當您逐行執行該方法時發生的情況。 –

+0

謝謝你的時間,我設法解決問題 – andyfed

回答

0

嘗試在終端集羣中的控制檯上寫一些東西。 可能的情況是拋出了一個異常,但沒有得到滿足。

但是,你會看到......不是。

我的第一個提示:調試你的程序,這是經常幫助我的。

但你也可以試試這個:

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"); 
       } 
    }` 

那是一件事,我會嘗試,以找出或者是什麼問題。 即使它沒有多大意義;)

+0

非常感謝你的時間和幫助,我最終能夠解決問題。我解釋瞭如何在另一評論,你可以去看看,如果你有興趣 – andyfed

0

那麼,你只捕獲IOException。在你的代碼中可能有RuntimeException。在這種情況下,你不會到達你的catch塊。

更改IOExceptionException您將看到原因。

+0

謝謝你的建議,我最終修復了問題 – andyfed

0

new ObjectInputStream()可以拋出IOException以外的異常,但是您的try-catch只捕獲IOException。如果拋出的異常是其他類型之一,會發生什麼?

+0

謝謝你的建議,我最終能夠解決這個問題,它涉及到程序在調用socket.getInputStream()時陷入困境,因爲我的服務器上發生了一個奇怪的問題,並且它無法實例化它的輸出流,因此socket.getInputStream()方法阻塞了該程序,因爲它無法找到輸出來自另一端的流 – andyfed

0

更換IOExceptionException類,這是所有的異常classes.Whatever的父母可能是例外,它一定會得到使用catch塊Exception類捕獲。

因此,在您的代碼中隨處可以替換catch (IOException ioe)這與catch (Exception ioe)。然後你可能會發現異常來自哪裏。

+0

感謝您的建議,我最終能夠解決這個問題。如果你有興趣,我寫了另一條評論 – andyfed