0

我正在尝试制作一个与我的 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 ----------
4

1 回答 1

0
public static String serverIP = "localhost";


public ClientChatHandler.....
    serverIP = host;

这是您的错误,您正在初始化变量serverIP,但是当您实例化该类时,您使用空字符串加载它。您在构造函数中有 2 个选项

  1. 删除变量host
  2. 调用构造函数时,作为参数发送"localhost"
于 2020-11-03T23:38:20.983 回答