枚举NullPointerException Java
我正在进行客户机-服务器通信,其中一条消息是枚举。要处理服务器端的枚举值并使用.toString()转换客户端的枚举值,在服务器端,根据接收到的值,创建的变量将获取枚举值。代码是服务器端的,错误在服务器协议上:枚举NullPointerException Java,java,nullpointerexception,communication,Java,Nullpointerexception,Communication,我正在进行客户机-服务器通信,其中一条消息是枚举。要处理服务器端的枚举值并使用.toString()转换客户端的枚举值,在服务器端,根据接收到的值,创建的变量将获取枚举值。代码是服务器端的,错误在服务器协议上: if(x < -type.getLeftInset(rotation) || x + type.getDimension() - type.getRightInset(rotation) >= 10) { return false; }
if(x < -type.getLeftInset(rotation) || x + type.getDimension() - type.getRightInset(rotation) >= 10) {
return false;
}
我认为这个错误可能是因为它没有初始化,但是在服务器协议代码的开头就初始化了。错误堆栈跟踪为:
Exception in thread "Thread-1" java.lang.NullPointerException
at Servidor.ServerProtocol.MsgValidate(ServerProtocol.java:224)
at Servidor.Server.run(Server.java:138)
at java.base/java.lang.Thread.run(Thread.java:830)
您面临的问题是,您正在使用检查引用相等性的
=
来比较java.lang.String
。
由于不满足任何条件,因此方法MsgValidate
中的变量type
将保持null
要解决此具体问题,请将所有检查替换为,例如,typeS.equals(TileType.TypeI.toString())
。然而,在您展示的代码中还有其他几个陷阱
陷阱
服务器协议:
type
。您只是创建了一个名为type
的局部变量,它没有被使用type
应该只是MsgValidate
方法中的局部变量平铺
数组,也从未对其进行写入。瓷砖永远不会被占用if
s序列可以替换为一个简单的for循环包装服务商;
导入Servidor.TileType;
//服务器端协议
公共类服务器协议{
私有静态最终TileType[]TILE\u TYPES=新TileType[]{
TileType.TypeI,TileType.TypeJ,TileType.TypeL,TileType.TypeO,TileType.TypeS,TileType.TypeT,TileType.TypeZ
};
//HighScore HighScore=null;
//私人高分房协;
DataPlayer DataPlayer;//=null;
私有瓷砖类型[][]瓷砖;
私有整数行、分数、新级别、级别;
公共服务器协议(){
}
公共布尔值MsgValidate(字符串类型、int x、int y、int旋转){
TileType type=null;
对于(int i=0;i=10){
返回false;
}
如果(y<-type.getTopInset(旋转)| | y+type.getDimension()-type.getBottomInset(旋转)>=22){
返回false;
}
对于(int col=0;col=瓷砖长度){
返回false;
}else如果(x<0 | | x>=tiles[y]。长度){
返回false;
}
返回瓷砖[y][x]!=null;
}
}
此外,您还应该阅读一些常见的Java代码约定。
例如,包应该是完全小写的。MethodName应以小写字母开头尝试使用调试器,或使用查找空值。错误在第224行,但您提供的代码只有92行。这将是一个很大的帮助,如果我们新的,在哪一行它崩溃。请用注释标记它:)您的解决方案解决了这个问题,但我在服务器协议的isOccupied(intx,inty)上遇到了另一个NullPointerException。我很想说那些坐标没有被占据。你知道如何解决那个问题吗?另外,你在代码中还发现了哪些陷阱?@Peter我又添加了一些陷阱和解释。希望对你有帮助你的笔记很有帮助。对于第三个,在与服务器连接时,tile在For循环中启动。现在,它运行时没有错误,但当服务器回答“有效移动”时,客户端不会继续执行代码,它应该在其中更新行并重新绘制。是否与连接时间有关?我认为这应该是可行的,因为来自服务器的答案存储在一个变量中,连接被关闭,如果答案有效,客户端将增加行并重新绘制。你知道可能是什么问题吗?不用管我把它修好了。你能相信我用“==”代替了.equals()?啊哈。谢谢你,你帮了我很大的忙!!!
package Servidor;
import Servidor.TileType;
public class Server implements Runnable {
public static final String HighScore = null;
public Socket server = null;
private TileType[][] tiles;
public Server(Socket socket){
this.server = socket;
}
public static void main (String[] args) throws IOException, ClassNotFoundException{
int port = 6666;
try(ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server is listening on port: " + port + "\n\n");
while(true) {
Socket socket = serverSocket.accept();
System.out.println("----------------------------------------------------------------------------------------------------");
System.out.println("Communication accepted: " + socket.getInetAddress().getHostAddress() /*+ "\n"*/);
Server server = new Server(socket);
Thread t = new Thread(server);
t.start();
}
}catch(Exception e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
ServerProtocol Comunicao = new ServerProtocol();
public void run() {
ObjectInputStream input;
try {
input = new ObjectInputStream(server.getInputStream());
String Msg = (String) input.readObject();
System.out.println(Msg);
if (Msg.equals("Start")) {
System.out.println("Start");
return;
}
if(Msg.equals("Validate")) {
String typeS = (String) input.readObject();
System.out.println("Type: " + typeS);
int x = (int) input.readObject();
System.out.println("X: " + x);
int y = (int) input.readObject();
System.out.println("Y: " + y);
int rotation = (int) input.readObject();
System.out.println("Rotation: " + rotation);
boolean answer = Comunicao.MsgValidate(typeS, x, y, rotation);
if(answer == false) {
System.out.println("Invalid move");
ObjectOutputStream saida = new ObjectOutputStream(server.getOutputStream());
saida.writeObject("Invalid move");
} else if(answer == true) {
System.out.println("Valid move");
ObjectOutputStream saida = new ObjectOutputStream(server.getOutputStream());
saida.writeObject("Valid move");
}
}
}catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Exception in thread "Thread-1" java.lang.NullPointerException
at Servidor.ServerProtocol.MsgValidate(ServerProtocol.java:224)
at Servidor.Server.run(Server.java:138)
at java.base/java.lang.Thread.run(Thread.java:830)