2014-12-06 41 views
0

當我使用ChatPanel.pw.println(ChatPanel.name +「已與聊天斷開連接」)時,我的主要問題是我得到一個nullpointerexception,而printwriter在ChatPanel類中, m試圖使用printwriter通過套接字發送消息。如果有人幫助我理解,也許給我一個解決問題的辦法。我刪除了很多代碼,但它應該編譯。使用另一個類的printwriter時遇到麻煩

import java.io.*; 
import java.awt.*; 
import java.net.*; 
import javax.swing.*; 
import java.awt.event.*; 
import javax.swing.JComponent; 
public class ChatFrame1 extends JFrame{ 


    public ChatFrame1(){ 
     setLayout(new GridBagLayout()); 
     setSize(1000,600); 
     setTitle("Chat"); 
     setResizable(true); 
     addWindowListener(new WindowAdapter(){ 

     public void windowClosing(WindowEvent we){ 

      //The problem is here. 
      ChatPanel.pw.println(ChatPanel.name + " has disconnected from the chat."); 
      System.out.println(ChatPanel.name); 
      System.exit(0); 
     } 
     }); 
     ChatPanel sp = new ChatPanel(); 
     setVisible(true); 
    } 
    public static void main(String[] args){ 
     new ChatFrame1(); 

    } 
} 
class ChatPanel extends JPanel implements ActionListener, Runnable{ 
    Thread t; 
    JTextField tf; 
    JTextArea ta; 
    JButton b; 
    Socket s; 
    BufferedReader br; 
    public static PrintWriter pw; 
    String temp; 
    boolean connected; 
    public static String name; 

    public ChatPanel(){ 

     GridBagConstraints gbc = new GridBagConstraints(); 
     name = (String)JOptionPane.showInputDialog(null,"Enter A Username "+ 
       "(Please Only Use Letters And Numbers) :", 
       "Username Login", 
       JOptionPane.PLAIN_MESSAGE, null, null, "User-Name"); 
     setLayout(new GridBagLayout()); 
     tf = new JTextField(); 
     tf.addActionListener(this); 
     ta = new JTextArea(); 
     b = new JButton("Press To Connect"); 
     b.addActionListener(this); 

    } 
    public void actionPerformed(ActionEvent ae){ 
     if((tf != null) && (!connected)){ 

     b.setLabel("Press to Connect"); 
     } 
     if((ae.getSource() == b) && (!connected)){ 
      try{ 
       s = new Socket("127.0.0.1", 2020); 
       pw = new PrintWriter(s.getOutputStream(), true); 
       tf.setText(""); 

      }catch(UnknownHostException uhe){ 
       System.out.println(uhe.getMessage()); 


      }catch(IOException ioe){ 

       System.out.println(ioe.getMessage()); 

      } 
      connected = true; 
      t = new Thread(this); 
      b.setLabel("Disconnect"); 
      t.start(); 

     }else if((ae.getSource() == b) && (connected)){ 
      connected = false; 
      try{ 
       pw.println(name +" has disconnected from the chat."); 
       ta.setText(""); 
       s.close(); //no buffering so, OK 
      }catch(IOException ioe){ 
       System.out.println(ioe.getMessage()); 
      } 
      b.setLabel("Press to Reconnect"); 
     }else{ 
     temp = tf.getText(); 
      tf.setText(""); 
     } 
    } 
    public void run(){ 

     try{ 
      BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 
      while(((temp = br.readLine()) != null) && connected){ 
      ta.append(temp + "\n"); 
      } 
     }catch(IOException ioe){ 
      System.out.println(ioe.getMessage()); 
     } 
    } 
} 

回答

0

我認爲這是給你的NullPointerException異常

ChatPanel.pw.println(ChatPanel.name + " has disconnected from the chat."); 

因爲ChatPanel.pw從來沒有在你的代碼被初始化,因此是無效

您應該確保ChatPanel.pw已經某處初始化(可能在構造函數中)在你的ChatPanel類中,然後調用ChatPanel.pw.println()

+0

我想我已經初始化'pw = new PrintWriter(s.getOutputStream(),true);' ChatPanel – Warlock1989 2014-12-06 08:15:56

+0

是的,它在你的'actionPerformed'函數中。 但是你確定在使用'ChatPanel.pw.println()'之前調用? – Gosu 2014-12-06 08:24:16

+0

謝謝,我已經添加了if語句,您對最後的評論是正確的。我不太確定它爲什麼不能正常工作,有時你只需要一雙新的眼睛。再次感謝。 – Warlock1989 2014-12-06 08:38:43