0
votes

It appears that I'm having some trouble. I'm attempting to read a random value in an enum, though I am running into nullpointers when trying to execute this. I'm not entirely sure what's wrong here, but hopefully someone could teach me what I'm doing wrong!

So, this is the error I get:

java.lang.NullPointerException at com.foxtrot.game.player.dialogues.impl.npcs.Child.run(Child.java:89) at com.foxtrot.game.player.DialogueManager.continueDialogue(DialogueManager.java:31) at com.foxtrot.net.decoders.WorldPacketsDecoder.processPackets(WorldPacketsDecoder.java:1127) at com.foxtrot.net.decoders.WorldPacketsDecoder.decode(WorldPacketsDecoder.java:282) at com.foxtrot.net.ServerChannelHandler.messageReceived(ServerChannelHandler.java:128) at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88) at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

This is the statement where I am receiving my error:

        if (player.getMonsterTask() == null) {
            player.getMonsterTask().applyTask();
            int amount = monsterHandler.getAmount();
            int id = player.getMonsterTask().getId();
            String name = NPCDefinitions.getNPCDefinitions(id).getName();
            send("Go, warrior! Go and kill ", "x"+amount+" of "+name+"!");
            return;
        }

player.getMonsterTask().applyTask(); is line 89.

public enum tasks {

    ROCK_CRAB(1265, 5, 50, 1, 7500),
    MAGIC_AXE(127, 5, 15, 1, 3000),
    CHAOS_DRUID(181, 5, 30, 1, 4000),
    MAN(2, 2, 10, 1, 1000),
    ROCK_GOLEM(3027, 1, 3, 1, 10000),
    GIANT_BAT(78, 5, 20, 1, 5000),
    OGRE(3419, 5, 60, 20, 12000),
    POISON_SPIDER(134, 10, 50, 25, 15000),
    CHAOS_DWARF(119, 5, 60, 30, 15000),
    CYCLOPS(6081, 15, 40, 35, 18000),
    ICE_WOLF(6052, 15, 40, 35, 19000),
    GIANT_SPIDER(2035, 15, 70, 40, 20000),
    HILL_GIANT(117, 15, 55, 45, 50000),
    MOSS_GIANT(1681, 20, 50, 47, 25000),
    GIANT_ROCK_CRAB(2885, 10, 90, 75, 75000);

    private int id, minAmount, maxAmount, levelReq, reward;
    private Player player;
    private MonsterHandler monsterHandler;
    public boolean completed = false;

    private static final List<tasks> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
    private static final int SIZE = VALUES.size();
    private static final Random RANDOM = new Random();

    tasks(int id, int minAmount, int maxAmount, int level, int reward) {
        this.id = id;
        this.minAmount = minAmount;
        this.maxAmount = maxAmount;
        this.levelReq = level;
        this.reward = reward;
    }

    public int getReward() {
        return reward;
    }

    public int getLevelReq() {
        return levelReq;
    }

    public int getId() {
        return id;
    }

    public int getMinAmount() {
        return minAmount;
    }

    public int getMaxAmount() {
        return maxAmount;
    }

    public void getMonsterTask() {
        player.setMonsterTask(VALUES.get(RANDOM.nextInt(SIZE)));
    }

    public boolean isComplete() {
        return completed;
    }

    public void setComplete(boolean b) {
        this.completed = b;
    }

    public void setReward(int r) {
        this.reward = r;
    }

    public void resetMonster(Player player) {
        player.getMonsterTask().setComplete(false);
        player.getMonsterTask().setNpcId(-1);
        player.getMonsterTask().setReward(-1);
        monsterHandler.setAmount(-1);
        player.setMonsterTask(null);
    }

    public void setNpcId(int id) {
        this.id = id;
    }           

}

That is my MonsterTasks class, where the applyTask method is being drawn from. Let me know if you need any more information!

Thank you.

4
if (player.getMonsterTask() == null) { player.getMonsterTask().applyTask(); You will always have a NullPointerException when you reach this line, as player.getMonsterTask() will be null. - Florent Bayle
It is entering the If , if (player.getMonsterTask() == null) { player.getMonsterTask().applyTask(); because player.getMonsterTask() is indeed null hence player.getMonsterTask().applyTask(); becomes null.applyTask() which throws the NullPointerException. - Clyde D'Cruz

4 Answers

2
votes

if (player.getMonsterTask() == null) this would mean that player.getMonsterTask() has actually returned null.
And in the next line player.getMonsterTask().applyTask(); you are actually doing a null.applyTask(). Hence the exception.

0
votes

You are trying to call a method on a null reference. That is only possible if the method is static, otherwise a NullPointerException will be thrown

0
votes

well, you ask if monster.getTask()==null and then call monster.getTask().applyTask() which is basically null.applyTask() ;) thats where the NPE comes from

0
votes

I think you have made a mistake in your design.

If you remove private Player player; from the task enum and change public void getMonsterTask() { to public static void getRandomMonsterTask(Player player) { things get clearer.

After some tinkering I get something like:

public enum tasks {

    ROCK_CRAB(1265, 5, 50, 1, 7500),
    MAGIC_AXE(127, 5, 15, 1, 3000),
    CHAOS_DRUID(181, 5, 30, 1, 4000),
    MAN(2, 2, 10, 1, 1000),
    ROCK_GOLEM(3027, 1, 3, 1, 10000),
    GIANT_BAT(78, 5, 20, 1, 5000),
    OGRE(3419, 5, 60, 20, 12000),
    POISON_SPIDER(134, 10, 50, 25, 15000),
    CHAOS_DWARF(119, 5, 60, 30, 15000),
    CYCLOPS(6081, 15, 40, 35, 18000),
    ICE_WOLF(6052, 15, 40, 35, 19000),
    GIANT_SPIDER(2035, 15, 70, 40, 20000),
    HILL_GIANT(117, 15, 55, 45, 50000),
    MOSS_GIANT(1681, 20, 50, 47, 25000),
    GIANT_ROCK_CRAB(2885, 10, 90, 75, 75000);

    private int id, minAmount, maxAmount, levelReq, reward;
    //private Player player;
    private MonsterHandler monsterHandler;
    public boolean completed = false;

    private static final List<tasks> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
    private static final int SIZE = VALUES.size();
    private static final Random RANDOM = new Random();

    tasks(int id, int minAmount, int maxAmount, int level, int reward) {
        this.id = id;
        this.minAmount = minAmount;
        this.maxAmount = maxAmount;
        this.levelReq = level;
        this.reward = reward;
    }

    public int getReward() {
        return reward;
    }

    public int getLevelReq() {
        return levelReq;
    }

    public int getId() {
        return id;
    }

    public int getMinAmount() {
        return minAmount;
    }

    public int getMaxAmount() {
        return maxAmount;
    }

    public static void getRandomMonsterTask(Player player) {
        player.setMonsterTask(VALUES.get(RANDOM.nextInt(SIZE)));
    }

    public boolean isComplete() {
        return completed;
    }

    public void setComplete(boolean b) {
        this.completed = b;
    }

    public void setReward(int r) {
        this.reward = r;
    }

    public void resetMonster(Player player) {
        player.getMonsterTask().setComplete(false);
        player.getMonsterTask().setNpcId(-1);
        player.getMonsterTask().setReward(-1);
        monsterHandler.setAmount(-1);
        player.setMonsterTask(null);
    }

    public void setNpcId(int id) {
        this.id = id;
    }

    private void applyTask() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

public void test() {
    Player player = new Player();
    if (player.getMonsterTask() == null) {
        tasks.getRandomMonsterTask(player);
    }
    player.getMonsterTask().applyTask();
}

Note that I have made some guesses as to your intent.