Enum NullPointerException Java

问题描述 投票:0回答:1

我正在进行一个客户端服务器通信,其中一个消息是一个枚举。在服务器端处理,并使用.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)
java nullpointerexception communication
1个回答
1
投票

你所面临的问题是由于你在比较一个 java.lang.String 使用 ==如果您的条件都不满足,那么变量 type 在你的方法中 MsgValidate 逗留 null.

为了解决这个具体问题,把你所有的 == 例如,检查与。typeS.equals(TileType.TypeI.toString()). 然而,在你展示的代码中还有其他一些陷阱。

陷阱

服务器协议。

  1. 你没有初始化成员变量。type 的构造函数中。你只是在构造函数中创建了一个名为 type,没有使用
  2. 成员价位be type 应该只是在你的 MsgValidate 办法
  3. 你从不初始化你的 tiles 数组,也永远不会向其写入。一个瓦片永远不会被
  4. 您的序列 if可以用一个简单的for-loop来代替。

应用建议的修改。

package Servidor;


import Servidor.TileType;



// Server side protocol

public class ServerProtocol {

    private static final TileType[] TILE_TYPES = new TileType[]{
        TileType.TypeI, TileType.TypeJ, TileType.TypeL, TileType.TypeO, TileType.TypeS, TileType.TypeT, TileType.TypeZ
    };

    //HighScore HighScore = null;
    //private HighScore hs;
    DataPlayer dataPlayer;// = null;

    private TileType[][] tiles;

    private int lines, score, newLevel, level;



    public ServerProtocol(){
    }



    public boolean MsgValidate(String typeS, int x, int y, int rotation) {
        TileType type = null;

        for (int i = 0; i < TILE_TYPES.length && type == null; i++) {
            if (typeS.equals(TILE_TYPES[i].toString())) {
                type = TILE_TYPES[i];
            }
        }

        if (type == null) {
            // or throw an IllegalArgumentException
            return false;
        }

        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) {
        if (tiles == null) {
            return false;
        } else if (y < 0 || y >= tiles.length) {
            return false;
        } else if (x < 0 || x >= tiles[y].length) {
            return false;
        }

        return tiles[y][x] != null;
    }
}

另外,你应该阅读一些常见的Java代码约定,例如,包应该完全小写。方法名应该以小写字母开始。

© www.soinside.com 2019 - 2024. All rights reserved.