[英]Handling exceptions Java
我正在尝试制作一个与我的 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 ----------
public static String serverIP = "localhost";
public ClientChatHandler.....
serverIP = host;
这是您的错误,您正在初始化变量serverIP
但是当您实例化类时,您使用空字符串加载它。 您在构造函数中有 2 个选项
host
或"localhost"
发送
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.