Peter
Peter

Reputation: 145

Enum NullPointerException Java

I am making a client server communication and one of the messages is a enum. To process of the server side and converted the enum value of the client using a .toString() and on the server side depending on the value received, a variable created gets a enum value. The code is of the server side and the error is on the server protocol:

if(x < -type.getLeftInset(rotation) || x + type.getDimension() - type.getRightInset(rotation) >= 10) {
            return false;
        }

The code for the server protocol is:

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;
    }


}

The server that calls the server protocol has the code:

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();
        }





    }



}

I think the error may be because it is nnot initialized but the did it in the beginning of the server protocol code. The error stack trace is:

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)

Upvotes: 0

Views: 108

Answers (1)

Felix
Felix

Reputation: 2396

The problem you're facing is because you're comparing a java.lang.String using ==, which checks for reference equality. Becuase none of your conditions is met, the variable type inside your method MsgValidate stays null.

To solve this concrete problem, replace all you == checks with, for example, typeS.equals(TileType.TypeI.toString()). However, there are several other pitfalls in the code you've shown.

Pitfalls

ServerProtocol:

  1. You're not initializing the member-variable type in the constructor. You're just creating a local variable named type, which is not used
  2. The member-varialbe type should just be a local variable inside your MsgValidate method
  3. You never inititialize your tiles array and also never write to it. A tile will never be occupied
  4. Your sequence of ifs can be replaced by a simple for-loop

Suggested changes applied:

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;
    }
}

Also, you should read about some common Java Code Conventions. For example, packages should be completly lowercase. Methodnames should start with an lowercase letter

Upvotes: 1

Related Questions