阅读枚举时的Nullpointer

时间:2015-06-04 08:27:36

标签: java enums

看来我遇到了麻烦。我试图在枚举中读取一个随机值,尽管我在尝试执行此操作时遇到了无效的指针。我不完全确定这里有什么问题,但希望有人可以告诉我我做错了什么!

所以,这是我得到的错误:

  

显示java.lang.NullPointerException       在com.foxtrot.game.player.dialogues.impl.npcs.Child.run(Child.java:89)       在com.foxtrot.game.player.DialogueManager.continueDialogue(DialogueManager.java:31)       在com.foxtrot.net.decoders.WorldPacketsDecoder.processPackets(WorldPacketsDecoder.java:1127)       在com.foxtrot.net.decoders.WorldPacketsDecoder.decode(WorldPacketsDecoder.java:282)       在com.foxtrot.net.ServerChannelHandler.messageReceived(ServerChannelHandler.java:128)       在org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88)       在org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)       在org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)       在org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)       在org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)       在org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)       在org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)       在org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318)       在org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)       在org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)       在org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)       在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)       在java.lang.Thread.run(未知来源)

这是我收到错误的声明:

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

}

这是我的MonsterTasks类,其中applyTask方法是从中抽取的。如果您需要更多信息,请与我们联系!

谢谢。

4 个答案:

答案 0 :(得分:2)

if (player.getMonsterTask() == null)这意味着player.getMonsterTask()实际上已返回null 在下一行player.getMonsterTask().applyTask();中,您实际上正在执行null.applyTask()。因此例外。

答案 1 :(得分:0)

您正尝试在空引用上调用方法。这只有在方法是静态的情况下才有可能,否则将抛出NullPointerException

答案 2 :(得分:0)

好吧,你问monster.getTask()==null然后打电话monster.getTask().applyTask()这基本上是null.applyTask();)那是NPE来自哪里

答案 3 :(得分:0)

我认为你的设计犯了错误。

如果您从private Player player;枚举中删除task并将public void getMonsterTask() {更改为public static void getRandomMonsterTask(Player player) {,事情会变得更加清晰。

经过一些修补后,我得到了类似的东西:

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

请注意,我已经对你的意图做了一些猜测。