我正在尝试使用对象发送消息。我有一些代码可以创建一个 GUI,而另一块代码可以在没有连接时创建服务器,或者在服务器可用时充当客户端。用户通过 GUI 键入消息并通过“通信器”将其作为“消息”对象发送。如果将通信器设置为客户端,则用户可以发送消息,但如果将其设置为服务器则不能...
如果用户尝试使用设置为服务器的程序发送消息,我会收到以下错误。
线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException
而且消息没有发送
这是我的通信器代码,底部是应该发送消息的函数
package hunterinstant;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Chris
*/
public class HunterCom implements Runnable {
private static String SERVER_IP = "127.0.0.1";
private static int SERVER_PORT = 5000;
Scanner scanner = new Scanner(System.in); //to read text from the console
Socket socket = null;
boolean client;
Conversation window;
ObjectOutputStream out = null;
public void initialize(Conversation c, String ip, String port) {
System.out.println(ip);
System.out.println(port);
SERVER_IP = ip;
SERVER_PORT = Integer.parseInt(port);
window = c;
}
public void run() {
try {
socket = new Socket(SERVER_IP, SERVER_PORT);
window.addText("Connected to server");
client = true;
} catch (Exception ex) {
window.addText("No Users Online: Waiting for connection");
client = false;
}
if (client == true) {
ObjectInputStream in = null;
while (true) {
try {
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
//get the reply from the server
if (in == null) {
in = new ObjectInputStream(socket.getInputStream());
}
Message message = (Message) in.readObject();
window.addText(message.getMessage());
//System.out.println("Server said: " + message.getMessage());
} catch (Exception ex) {
window.addText("Error: " + ex);
}
}
} // End Client
else {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(SERVER_PORT);
} catch (IOException ex) {
window.addText("Error occured while creating the server socket");
return;
}
Socket socket = null;
try {
//Waits untill a connection is made, and returns that socket
socket = serverSocket.accept();
} catch (IOException ex) {
window.addText("Error occured while accepting the socket");
return;
}
window.addText("Connection created, client IP: " + socket.getInetAddress());
ObjectInputStream in = null;
while (true) {
try {
if (in == null) {
in = new ObjectInputStream(socket.getInputStream());
}
Message message = (Message) in.readObject();
window.addText(message.getMessage());
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
} catch (Exception ex) {
window.addText("Error: " + ex);
}
}
}
}
//THIS IS THE CODE THAT IS SUPPOSED TO SEND THE MESSAGE
public void sendMessage(String str)
{
try {
out.writeObject(new Message(str));
System.out.println("Hello");
out.flush();
} catch (IOException ex) {
Logger.getLogger(HunterCom.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是应该从 GUI 发送消息的代码
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
comunicator.sendMessage(jTextField1.getText());
jTextArea2.append(jTextField1.getText());
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==10) // Has the user pressed enter?
{
comunicator.sendMessage(jTextField1.getText());
jTextArea2.append(jTextField1.getText());
}
}
对不起,我确定这是一个愚蠢的问题......我确定我在这里做的事情显然很愚蠢,但我只做了几个月的 Java,我花了几个小时试图弄清楚这个问题没有喜悦....