使用另一个类的printwriter时出现问题

时间:2014-12-06 00:13:53

标签: java class sockets global printwriter

我的主要问题是,当我使用ChatPanel.pw.println(ChatPanel.name +"已脱离聊天。")时,我得到一个nullpointerexception,而printwriter在ChatPanel类中,我&# 39; m试图使用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());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这会给你NullPointerException

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

因为ChatPanel.pw从未在代码中初始化,因此为空

在调用ChatPanel.pw

之前,您应该确保ChatPanel类中ChatPanel.pw.println()已初始化(可能在构造函数内)