我正在尝试制作一个聊天服务器,它可以与我的Min克拉夫特客户端进行通信,并且工作正常,但我无法让客户端与服务器一起工作。这是客户
public class ClientChatHandler {
public static ObjectOutputStream output;
public static ObjectInputStream input;
public static String message = "";
public static String serverIP = "localhost";
public static Socket connection;
}
public ClientChatHandler(String host){
serverIP = host;
}
public static void startRunning(){
try{
connectToServer();
setupStreams();
whileChatting();
} catch(EOFException eofException){
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + "\247a[Server] Client terminated the connection.");
} catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void connectToServer() throws IOException{
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + "\247a[Server] Attempting to connect.");
connection = new Socket(InetAddress.getByName(serverIP), 1337);
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + "\247a[Server] Connected to server.");
}
public static void setupStreams() throws IOException{
try{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
} catch(NullPointerException Exception){
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + "\247a[Server] Error creating streams. Closing connection");
} catch(IOException ioException){
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + "\247a[Server] Error creating streams. Closing connection");
}
}
public static void whileChatting() throws IOException{
do{
try{
message = (String) input.readObject();
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + message);
}catch(ClassNotFoundException classNotFoundException){
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + "\247a[Server] \2474ERROR! Connecting, closing sockets.");
}
}while(!message.equals("server - end"));
}
public static void closeCrap(){
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + "\247a[Server] Closing connection");
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void sendMessage(String message){
try{
output.writeObject(message);
output.flush();
Minecraft.getMinecraft().thePlayer.addChatMessage(Variables.color + message);
} catch(IOException ioException){
}
}
public static void showMessage(final String message){
Minecraft.getMinecraft().thePlayer.addChatMessage(Methods.getLetterColor(Variables.ChatName Color) + Minecraft.getMinecraft().thePlayer.username + "\2477:\247f " + message);
}现在我不断地得到这方面的java.lang.NullPointerException:
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());我如何解决这个问题?
很抱歉,缺少信息,但是“我的世界”崩溃了,我从上面的代码中得到了一个空指针异常。
以下是一些文本:
java.lang.NullPointerException
at net.minecraft.src.ClientChatHandler.whileChatting(ClientChatHandler.Java:79)
at org.renivivious.GuiConsole.mouseClicked(GuiConsole.java:671)
at net.minecraft.src.GuiScreen.handleMouseInput(GuiScreen.java:198)
at org.renivivious.GuiConsole.handleMouseInput(GuiConsole.java:356)
at net.minecraft.src.GuiScreen.handleInput(GuiScreen.java:172)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1394)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:731)
at net.minecraft.client.Minecraft.run(Minecraft.java:656)
at java.lang.Thread.run(Unknown Source)
--- END ERROR REPORT 8e0635c8 ----------发布于 2020-11-03 23:38:20
public static String serverIP = "localhost";
public ClientChatHandler.....
serverIP = host;这里是您的错误,您正在初始化变量serverIP,但是当您实例化类时,您使用一个空字符串加载它。构造函数中有两个选项。
host或"localhost"的形式发送https://stackoverflow.com/questions/15992090
复制相似问题