0

So, im trying to make a snake and ladders game. The way im trying to do it is by creating a board which has nodes, the first node (head) will have a node next to it, and this one will have this one as a previous node. This repeats until it gets to the total number of nodes (tha last node doesnt have a next node). The way im trying to make the snakes and ladders is by creating a relation of snakeHead and snakeTail or ladderTop and ladderBottom. I then generate a random number which will represent a specific node on the board but for some reason when i try to create the relation i get a nullPointerException on the method createSnakes, i dont get why this is happening so if someone has an idea of why it happens please tell me. Thanks in advance

public class Board {

    private int numRows;
    private int numColumns;
    private int dimension;
    private int numSnakes;
    private int numLadders;
    private Node head;
    private Node tail;

    /**
     * This is the constructor
     * @param numRows
     * @param numColumns
     */
    public Board(int numRows, int numColumns, int numSnakes, int numLadders) {
        this.dimension = numColumns * numRows;
        this.numLadders = numLadders;
        this.numSnakes = numSnakes;
        this.numRows = numRows;
        this.numColumns = numColumns;
        head = new Node(1);
        createBoard(head, 2);
        createSnakes(0);
        createLadders(0);
    }

    /**
     * This method create board
     * @param prev this is the prev node
     * @param i this is the counter
     */
    private void createBoard(Node prev, int i){
        if(i <= dimension){
            if(i != dimension) {
                Node current = new Node(i);
                prev.setNext(current);
                current.setPrev(prev);
                createBoard(current, i + 1);
            } else {
                tail = new Node(i);
                prev.setNext(tail);
                tail.setPrev(prev);
            }
        }
    }

    private void createSnakes(int i){
        if(i<numSnakes){
            int initPosition = getInitPosition();
            Node init = get(initPosition,1, head);

            int endPosition = (int) (Math.random() * initPosition);
            Node end = get(endPosition, 1, head);
            if(init.getSnakeTail() == null && init.getSnakeHead() == null && end.getSnakeHead() == null && end.getSnakeTail() == null
            && verifyIntervals(initPosition, endPosition, 1, 0, false, false, false) == false){
                init.setSnakeTail(end);
                end.setSnakeHead(init);
                init.setSnake((char)(i+65));
                end.setSnake((char)(i+65));
                createSnakes(i + 1);
            } else {
                createSnakes(i);
            }
        }
    }

    private void createLadders(int i){
        if(i<numLadders){
            int initPosition = getInitPosition();
            if(initPosition==0){
                createLadders(i);
            }
            Node init = get(initPosition,1, head);

            int rank = dimension-initPosition;

            int endPosition = (int) (Math.random()*rank);
            if(endPosition==0 || endPosition==dimension){
                createLadders(i);
            }
            Node end = get(endPosition+initPosition, 1, head);

            if(i==numLadders){
                return;
            }
            if(init.getSnakeTail() == null && end.getSnakeHead() == null && init.getSnakeHead() == null && end.getSnakeTail() == null 
                    && init.getLadderTop() == null && end.getLadderBottom() == null && init.getLadderBottom() == null && end.getLadderTop() == null
               && verifyIntervals(initPosition, endPosition, 1, 0, false, false, false) == false) {
                init.setLadderTop(end);
                end.setLadderBottom(init);
                init.setLadder((char) (i + 48));
                end.setLadder((char) (i + 48));
                createLadders(i + 1);
            }
            else {
                createLadders(i);
            }
        }
    }

    public boolean verifyIntervals(int init, int end, int varInitPos, int initPos, boolean initFound, boolean endFound,boolean inInterval){
        if(inInterval==true){
            return true;
        }
        if(varInitPos==dimension){
            return false;
        }
        else if(varInitPos<initPos+numRows){
            if(init==varInitPos){
                initFound=true;
            }
            if(end==varInitPos){
                endFound=true;
            }
            if(initFound==true && endFound==true){
                inInterval=true;
            }
            return verifyIntervals(init,end,varInitPos+1,initPos,initFound,endFound,inInterval);
        }
        else{
            return verifyIntervals(init,end,varInitPos,initPos+numRows,false,false, false);
        }
    }

    public Node getTail() {
        return tail;
    }

    public Node get(int position, int i, Node current){
        if(i == position){
            return current;
        } else if(i==dimension) {
            return null;
        } else {
            current = current.getNext();
            return get(position, i+1, current);
        }
    }

    public Node getHead() {
        return head;
    }

    private int getInitPosition(){
        return (int) (Math.random()*dimension);
    }

    public int getNumRows() {
        return numRows;
    }

    public int getNumColumns() {
        return numColumns;
    }

    public int getDimension() {
        return dimension;
    }

    public int getNumSnakes() {
        return numSnakes;
    }

    public int getNumLadders() {
        return numLadders;
    }
}
  • What is the line number of the exception? – Sid Nov 07 '21 at 19:54
  • Pretty tough to say why this is happening without the line number. The initialization of a Node in your get() method can return a null value. You are accessing these variables at a later stage which could throw the exception. – lukas Nov 07 '21 at 20:02

1 Answers1

0

i get a nullPointerException on the method createSnakes, i dont get why this is happening so if someone has an idea of why it happens please tell me.

A NullPointerException is thrown when you are trying to call a method on an object reference, which has a value of null.

String str = null
str.substring(1) // This will throw NPE, because str == null

To solve this problem you need to understand which object is null and make sure it is properly initialised in the code before it is ever used.

In your case it probably has to do with random number generation aspect of the code ... you might not be generating enough objects/numbers and because of this some things are uninitialised.

To achieve this you can either use a debugger or print out values of variables you suspect might contain null (hint: use the line number from the exception message) or print them out using System.out.ln or a logging library.

Also, take a look at this

Arthur Klezovich
  • 2,595
  • 1
  • 13
  • 17