2016-04-07 70 views
2

我寫了一個小型聊天程序,該程序由3個類組成:TalkServer(服務器),TalkDienst(管理客戶端之間數據交換的線程類)和ChatProgram(客戶端)。readLine()在聊天程序中拋出異常,我不知道爲什麼

TalkDienst:

public class TalkDienst extends Thread { 
    private Socket send, rec; 
    private BufferedReader in; 
    private PrintWriter out; 
    private boolean sendClosed, recClosed; 

    public TalkDienst(Socket send, Socket rec) { 
     this.send = send; 
     this.rec = rec; 

     try { 
      this.in = new BufferedReader(new InputStreamReader(this.rec.getInputStream())); 
      this.out = new PrintWriter(new OutputStreamWriter(this.send.getOutputStream()), true); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     this.start(); 
    } 

    @Override 
    public void run() { 
     // this.out.println("--- Partner found ---"); 
     System.out.println("Partner found"); 
     while (true) { 
      try { 
       String input = this.in.readLine(); 
       System.out.println("test"); 
       if (!input.equals(null)) { 
        this.out.println(input); 
       } 
      } catch (IOException e) { 
       closeall(); 
       e.printStackTrace(); 
       break; 
      } catch (NullPointerException e) { 
       closeall(); 
       e.printStackTrace(); 
       break; 
      } 
     } 
    } 

    private void closeall() { 
     try { 
      this.send.close(); 
      this.rec.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

ChatProgram:

public class ChatProgramm extends JFrame implements Runnable 
{ 
    private Thread t; 
    private Socket s; 
    private Container c; 
    private JTextArea ta; 
    private JScrollPane sp; 
    private JScrollBar sb; 
    private JTextField tf; 

    private BufferedReader in; 
    private PrintWriter out; 

    private boolean socketClosed; 

    public ChatProgramm(Socket h) 
    { 
     this.t = new Thread(this); 

     this.s = h; 
     try 
     { 
      this.in = new BufferedReader(new InputStreamReader(this.s.getInputStream())); 
      this.out = new PrintWriter(new OutputStreamWriter(this.s.getOutputStream()), true); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     this.t.start(); 
//Just some UI things...[...] 

     this.ta = new JTextArea(); 
     this.ta.setEditable(false); 
     this.ta.setText("--- Waiting for partner ---"); 

     this.tf = new JTextField(); 
     this.tf.addActionListener(new ActionListener() 
     { 

      @Override 
      public void actionPerformed(ActionEvent arg0) 
      { 
       if(tf.getText().length() > 0) 
       { 
        out.println(">>>: " + tf.getText()); 
        ta.setText(ta.getText() + "\n<<<: " + tf.getText()); 
        tf.setText(""); 
       } 
      } 

     }); 

     this.c.add(this.sp); 
     this.c.add(this.tf); 

    @Override 
    public void run() 
    { 
     while(true) 
     { 
      try 
      { 
       String input = this.in.readLine(); 
       System.out.println("warten"); 
       if(!input.equals(null)) 
       { 
        this.ta.setText(this.ta.getText() + "\n" + input); 
        this.sb.validate(); 
        this.sb.setValue(this.sb.getMaximum()); 
       } 
      } catch (IOException e) 
      { 
       closeall(); 
       e.printStackTrace(); 
       break; 
      } catch (NullPointerException e) 
      { 
       closeall(); 
       e.printStackTrace(); 
       break; 
      } 
     } 
    } 

    private void closeall() 
    { 
     try 
     { 
      this.s.close(); 
      this.ta.setText(this.ta.getText() + "\n--- Connection lost ---"); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

當我刪除

註釋// this.out.println(「---合作伙伴發現 - - 「);

在TalkDienst

,那麼程序不需額外的工作,並給了我這些錯誤消息:左邊窗口顯示TalkDienst,右上方是客戶端1和右下角的客戶端2

Error messages and clients

這裏有在文本形式的請求的錯誤消息: TalkServer:

java.net.SocketException: Socket closed 
     at java.net.SocketInputStream.socketRead0(Native Method) 
     at java.net.SocketInputStream.socketRead(Unknown Source) 
     at java.net.SocketInputStream.read(Unknown Source) 
     at java.net.SocketInputStream.read(Unknown Source) 
     at sun.nio.cs.StreamDecoder.readBytes(Unknown Source) 
     at sun.nio.cs.StreamDecoder.implRead(Unknown Source) 
     at sun.nio.cs.StreamDecoder.read(Unknown Source) 
     at java.io.InputStreamReader.read(Unknown Source) 
     at java.io.BufferedReader.fill(Unknown Source) 
     at java.io.BufferedReader.readLine(Unknown Source) 
     at java.io.BufferedReader.readLine(Unknown Source) 
     at TalkDienst.run(TalkDienst.java:40) 
java.lang.NullPointerException 
     at TalkDienst.run(TalkDienst.java:42) 

ChatProgramm 1:(開始第一)

java.lang.NullPointerException 
     at ChatProgramm.run(ChatProgramm.java:131) 
     at java.lang.Thread.run(Unknown Source) 

ChatProgramm 2:

Exception in thread "Thread-1" java.lang.NullPointerException 
     at ChatProgramm.closeall(ChatProgramm.java:156) 
     at ChatProgramm.run(ChatProgramm.java:144) 
     at java.lang.Thread.run(Unknown Source) 

我感謝你的幫助,在此先感謝!

+0

請複製並粘貼命令提示符窗口中的文本,而不是鏈接到圖像。 –

+0

好的,我複製了異常! –

回答

0

println()不會拋出異常,包括這個由readLine()引發的異常,因爲您關閉了套接字並繼續使用它。

您的流檢查結束也是錯誤的。通常的檢查是:

while ((line = in.readLine()) != null 
    // ... 
+0

我沒有得到如何關閉插座,並繼續使用它!你能解釋一下嗎? –

相關問題