我正在尝试制作一个与我的 Minecraft 客户端通信并且运行良好的聊天服务器,但我无法让客户端与服务器一起工作。这是客户端
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 ----------