-2

i cannot fix this null pointer exception, nor could my prof. i have commented out and worked on it for a while but it isnt working for some reason! the error is: null pointer exception, i can post the actual error if that helps

 public static void main(String[] args) {
    // initialize character and world
    Player player = new Player();
    map = new Map();
    Jf = new JFrameManager();
    Jf.addToOutput("Mage or Knight?");
    do {
        player.setPlayerType(Jf.getCommand());
        Jf.setClass(player.getPlayerHeader());
    } while (!("Mage".equalsIgnoreCase(player.getPlayerType()) || "Knight"
            .equalsIgnoreCase(player.getPlayerType())));
    Jf.addToOutput("You Have Selected " + player.getPlayerType() + ", "
            + player.getPlayerName());
    Jf.clearCommand();
    while (true) {
        nextCommand(Jf.getCommand());
    }

}

public static void nextCommand(String commandInput) {
    String command = null;
    String constructor = null;
    String[] str_array;
    if (!commandInput.equals("")) {
        str_array = commandInput.split(" ");
        command = str_array[0];
        constructor = str_array[1];
    } else {
        command = "";
    }
    switch (command.toLowerCase()) {
    case "move":
        if (isValidMove(constructor)) {
            player.move(constructor);
        }
        break;
    case "equipmain":
        player.setWeapon(1, player.getItemNumber(constructor));
        break;
    case "equipoff":
        player.setWeapon(2, player.getItemNumber(constructor));
        break;
    case "equiparmor":
        player.setArmor(player.getItemNumber(constructor));
        break;
    case "trash":
        player.deleteFromInv(constructor);
        break;
    }
}

public static void initiateEventMove(String direction) {
    if (isValidMove(direction)) {
        Jf.addToOutput("You have Moved " + direction);
        player.move(direction);
        switch (map.getEvent(player.getPosition())) {
        case 2:
            intiateAIFight();
            break;
        }
    }

}

public static boolean isValidMove(String direction) {
    boolean r = false;
    switch (direction.toLowerCase()) {
    case "up":
        if ((((player.getPosition() - 20) / 20) >= 1)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    case "down":
        if ((((player.getPosition() + 20) / 20) >= 1)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    case "left":
        if ((((player.getPosition() - 1) % 20) != 0)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    case "right":
        if (((player.getPosition() % 20) != 0)
                && (map.getEvent(player.getPosition()) != 0)) {
            r = true;
        }
        ;
        break;
    }
    return r;
}

public static void intiateAIFight() {
    mob = new AI("mixed", 0, False);
    while ((mob.isDead() != true) && (player.isDead() != true)) {
        mob.takeDamage(player.attack());
        Jf.addToOutput("You gave the " + mob.getType() + player.attack());
        player.takeDamage(mob.attack());
        Jf.addToOutput("You recieved " + mob.attack()
                + " dammage from the " + mob.getType());
    }
}

}

WIll
  • 45
  • 6
  • 10
    Please post a stacktrace or narrow down your code to point of exception – Darshan Lila Dec 03 '14 at 13:47
  • change your prof if they cant fix it?! lol...shocking – Harry Dec 03 '14 at 13:49
  • possible duplicate of [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) – StackFlowed Dec 03 '14 at 13:51
  • I suggest stripping down the program, getting rid of everything that is not needed to reproduce the error. You may find out what is wrong doing that, but if you don't the result should be a complete program you can post. – Patricia Shanahan Dec 03 '14 at 13:56

2 Answers2

0

There are multiple places where nullpointers can occur but there are insufficient informations in you post.

I guess the probable reason is that in your main method you define a new local variable:

Player player = new Player();

But in your method nextCommand() you try to access a global variable "player" that you didn´t instanciate before.

For example:

player.setWeapon(1, player.getItemNumber(constructor));

So my guess is you have to change the line

Player player = new Player();

to

player = new Player();

to instanciate your global variable.

Next reason could be that at

nextCommand(Jf.getCommand())

your Jf.getCommand() returns null and you call you nextCommand-method with the parameter null.

In that method you compare this string with another string without checking if it´s is null.

if (!commandInput.equals("")) {
    str_array = commandInput.split(" ");
    command = str_array[0];
    constructor = str_array[1];
}

There also can occur a nullpointer exception

As said, just two possibiblitys of many. You have to add the stacktrace and the complete class for finding the right one

maimArt
  • 389
  • 1
  • 11
0

For code:

public static void nextCommand(String commandInput) {
    String constructor = null;
    ...
    if (!commandInput.equals("")) {
    ...
    } else {
        command = "";
    }
    ...
    if (isValidMove(constructor)) {

The "isValidMove(constructor)" throws NullPointerException, if "commandInput.equals("")", because in "isValidMove(constructor)" you use "direction.toLowerCase()"

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59