2014-12-06 56 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