我正在創建一種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()。
謝謝
添加斷點和調試。 – Mena
嘗試調試您的代碼,查看當您逐行執行該方法時發生的情況。 –
謝謝你的時間,我設法解決問題 – andyfed