1

I am trying to create a 2D game in Java where a user is prompted for a row width and column length. A 'world' object is created that looks like this when printed (where P is the player's character):

P---
----
----
----

The user can then enter up, down, left, right or exit which should move the P around and do nothing if the move would move the character off the map. My issue is after I enter the width and length, I get the following error:

java.lang.NullPointerException
at World.displayWorld(Driver.java:90)
at Driver.main(Driver.java:28)

I believe this means that Java is seeing worldDimensions.length as empty/null?? I thought I was assigning a value to it when the world object is created.. Any guidance is much appreciated!

Here's my code:

import java.util.Scanner;

public class Driver {
    public static void main(String args[]) {

        //Create a scanner object
        Scanner input = new Scanner(System. in );

        boolean exit = true;

        //Prompt user to enter world width and height
        System.out.print("World width: ");
        int userWidth = input.nextInt();
        System.out.print("World height: ");
        int userHeight = input.nextInt();

        //Create a world object
        World world1 = new World(userWidth, userHeight);
        world1.displayWorld();

        System.out.println("Possible inputs: up, down, left, right, exit");

        while (exit = true) {
            //display world
            world1.displayWorld();

            System.out.print("ACTION > ");
            String userAction = input.next();

            if (userAction == "up" || userAction == "down" || userAction == "left" || userAction == "right" || userAction == "exit") {
                switch (userAction) {
                    case "up":
                        world1.moveUp();
                        break;
                    case "down":
                        world1.moveDown();
                        break;
                    case "left":
                        world1.moveLeft();
                        break;
                    case "right":
                        world1.moveRight();
                        break;
                    case "exit":
                        exit = false;
                        break;
                }
            } else {
                System.out.println("Invalid Input. Possible inputs are: up, down, left, right, or exit.");
            }
        }

    }
}

class World {
    static private char[][] worldDimensions;
    static private int characterRow;
    static private int characterColumn;

    public World(int userWidth, int userHeight) {
        char[][] worldDimensions = new char[userHeight][userWidth];
        int characterRow = 0;
        int characterColumn = 0;

        for (int row = 0; row < worldDimensions.length; row++) {
            for (int column = 0; column < worldDimensions[row].length; column++) {
                if (characterRow == row && characterColumn == column) {
                    worldDimensions[row][column] = (char)
                    'P';
                } else {
                    worldDimensions[row][column] = (char)
                    '-';
                }
            }
        }
    }

    public void displayWorld() {
        for (int row = 0; row < worldDimensions.length; row++) {
            for (int column = 0; column < worldDimensions[row].length; column++) {
                System.out.print(worldDimensions[row][column]);
            }
            System.out.println();
        }
    }

    public void moveUp() {
        if (characterRow - 1 >= 0) {
            characterRow--;
        }
    }

    public void moveDown() {
        if (characterRow + 1 <= worldDimensions[0].length) {
            characterRow++;
        }
    }

    public void moveLeft() {
        if (characterColumn - 1 >= 0) {
            characterColumn--;
        }
    }

    public void moveRight() {
        if (characterRow + 1 <= worldDimensions.length) {
            characterRow++;
        }
    }
}
Mark Sholund
  • 1,273
  • 2
  • 18
  • 32
JimBob117
  • 99
  • 2
  • 8
  • 2
    Use [`equals() to compare strings`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Also check [`What is a Null Pointer Exception, and how do I fix it?`](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – sam Nov 04 '15 at 18:55
  • For future reference, its useful to indicate which line the null pointer is on - as opposed to us having to find it. – Sh4d0wsPlyr Nov 04 '15 at 18:57
  • 3
    There are at least three mistakes here - using `=` instead of *just* `exit`, using `==` to compare Strings, and the redundant cast to `char`... – Makoto Nov 04 '15 at 18:59
  • Change "char[][] worldDimensions = ..." for just "worldDimensions = ..." at the first line of World class. – cesarse Nov 04 '15 at 19:00
  • You could get rid of the if-else statement surrounding the switch statement and just add a `default` case. – Monkeygrinder Nov 04 '15 at 19:56
  • Thanks for all the comments everyone! It helped me a ton. – JimBob117 Nov 04 '15 at 23:36

2 Answers2

1

You're shadowing worldDimensions inside of your constructor. The local declaration does not give you the same field as previously declared.

Simply remove the declaration and you'll be alright (at least for that error):

worldDimensions = new char[userHeight][userWidth];
Makoto
  • 104,088
  • 27
  • 192
  • 230
1

You're overwriting your class variable worldDimensions. Your World constructor should look like the following

public World(int userWidth, int userHeight)
{
//here is where you were overwriting the global variable, leaving it null
//and populating the local one instead
worldDimensions = new char[userHeight][userWidth];
int characterRow = 0;
int characterColumn = 0;

for (int row = 0; row < worldDimensions.length; row++)
    {
    for (int column = 0; column < worldDimensions[row].length; column++)
        {
        if (characterRow == row && characterColumn == column)
            {
            worldDimensions[row][column] = (char)'P';
            }
        else
            {
            worldDimensions[row][column] = (char)'-';
            }
        }
    }
}
Brandon Laidig
  • 430
  • 2
  • 14