【发布时间】:2020-05-25 16:09:34
【问题描述】:
我正在进行客户端服务器通信,其中一条消息是枚举。为了处理服务器端并使用 .toString() 转换客户端的枚举值,并且在服务器端根据接收到的值,创建的变量获取枚举值。代码是服务器端的,错误在服务器协议上:
if(x < -type.getLeftInset(rotation) || x + type.getDimension() - type.getRightInset(rotation) >= 10) {
return false;
}
服务端协议代码为:
package Servidor;
import Servidor.TileType;
// Server side protocol
public class ServerProtocol {
//HighScore HighScore = null;
//private HighScore hs;
DataPlayer dataPlayer;// = null;
private TileType[][] tiles;
private int lines, score, newLevel, level;
private static TileType type;
public ServerProtocol(){
TileType type = TileType.TypeJ;
}
public boolean MsgValidate(String typeS, int x, int y, int rotation) {
if(typeS == TileType.TypeI.toString()) {
type = TileType.TypeI;
}
if(typeS == TileType.TypeJ.toString()) {
type = TileType.TypeJ;
}
if(typeS == TileType.TypeL.toString()) {
type = TileType.TypeL;
}
if(typeS == TileType.TypeO.toString()) {
type = TileType.TypeO;
}
if(typeS == TileType.TypeS.toString()) {
type = TileType.TypeS;
}
if(typeS == TileType.TypeT.toString()) {
type = TileType.TypeT;
}
if(typeS == TileType.TypeZ.toString()) {
type = TileType.TypeZ;
}
if(x < -type.getLeftInset(rotation) || x + type.getDimension() - type.getRightInset(rotation) >= 10) {
return false;
}
if(y < -type.getTopInset(rotation) || y + type.getDimension() - type.getBottomInset(rotation) >= 22) {
return false;
}
for(int col = 0; col < type.getDimension(); col++) {
for(int row = 0; row < type.getDimension(); row++) {
if(type.isTile(col, row, rotation) && isOccupied(x + col, y + row)) {
return false;
}
}
}
return true;
}
private boolean isOccupied(int x, int y) {
return tiles[y][x] != null;
}
}
调用服务器协议的服务器有代码:
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)
【问题讨论】:
-
尝试使用调试器,或使用helpful NPE 找出什么是空的。
-
错误在第 224 行,但您提供的代码只有 92 行。如果我们是新的,它将在哪条线上崩溃,这将是一个很大的帮助。请用评论标记它:)
标签: java nullpointerexception communication