我必须做一个聊天客户端/服务器项目,但我在ChatServer1类的pr1.println(msg)
上得到一个Null Pointer Exception。
任何帮助将不胜感激。
public class ChatClient1 extends JFrame{
private JPanel contentPane;
private JPanel panel_1 = new JPanel();
private static JTextArea textArea = new JTextArea();
private static JTextField Txt1 = new JTextField();
private JButton DisconnectBtn = new JButton("DISCONNECT");
private static JButton SendTxt = new JButton("SEND");
private JLabel lbl1 = new JLabel("Message to send");
ServerSocket serversocket;
static BufferedReader br1;
static PrintWriter pr1;
Socket socket;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChatServer1 frame = new ChatServer1();
frame.setVisible(true);
frame.setTitle("CLIENT");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChatClient1() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 542, 383);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel_1.setBackground(Color.GRAY);
panel_1.setBounds(0, 0, 536, 355);
contentPane.add(panel_1);
panel_1.setLayout(null);
DisconnectBtn.setBounds(29, 220, 183, 33);
panel_1.add(DisconnectBtn);
textArea.setBounds(235, 11, 291, 242);
panel_1.add(textArea);
Txt1.setBounds(29, 303, 387, 41);
panel_1.add(Txt1);
Txt1.setColumns(10);
SendTxt.setBounds(437, 303, 89, 41);
panel_1.add(SendTxt);
lbl1.setBounds(29, 278, 123, 14);
panel_1.add(lbl1);
try {
Socket socket = new Socket("LocalHost" , 5000);
pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
br1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
textArea.append("Server: " + br1.readLine() + '\n' );
}
} catch (IOException e) {
}
SendTxt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg = Txt1.getText();
pr1.println(msg);
pr1.flush();
textArea.append(msg + '\n');
Txt1.setText("");
}
});
}
}
ChatServer1类:
public class ChatServer1 extends JFrame{
private JPanel contentPane;
private JPanel panel_1 = new JPanel();
private static JTextArea textArea = new JTextArea();
private static JTextField Txt1 = new JTextField();
private JButton DisconnectBtn = new JButton("DISCONNECT");
private static JButton SendTxt = new JButton("SEND");
private JLabel lbl1 = new JLabel("Message to send");
static BufferedReader br1;
static PrintWriter pr1;
Socket socket;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChatServer1 frame = new ChatServer1();
frame.setVisible(true);
frame.setTitle("SERVER");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChatServer1() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 542, 383);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel_1.setBackground(Color.GRAY);
panel_1.setBounds(0, 0, 536, 355);
contentPane.add(panel_1);
panel_1.setLayout(null);
DisconnectBtn.setBounds(29, 220, 183, 33);
panel_1.add(DisconnectBtn);
textArea.setBounds(235, 11, 291, 242);
panel_1.add(textArea);
Txt1.setBounds(29, 303, 387, 41);
panel_1.add(Txt1);
Txt1.setColumns(10);
SendTxt.setBounds(437, 303, 89, 41);
panel_1.add(SendTxt);
lbl1.setBounds(29, 278, 123, 14);
panel_1.add(lbl1);
try {
ServerSocket serversocket = new ServerSocket(5000);
socket = serversocket.accept();
pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
br1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
textArea.append("Client: " + br1.readLine() + '\n' );
}
} catch (IOException e) {
}
SendTxt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg = Txt1.getText();
pr1.println(msg);
pr1.flush();
textArea.append(msg + '\n');
Txt1.setText("");
}
});
}
}
答案 0 :(得分:0)
你的问题在于ChatServer1代码,这里有以下内容:
ServerSocket serversocket = new ServerSocket(5000); socket = serversocket.accept();
当你调用accept时,它正在等待和监听传入的连接,所以从来没有实际到达你的pr1实例。因此pr1保持为null。易于修复只需将pr1实例移动到接受调用之上,如下所示:
DELETE FROM your_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM your_table
GROUP BY column1, column2, column3);
希望这有帮助!