读取枚举时为null指针



我似乎遇到了一些麻烦。我正在尝试读取枚举中的随机值,尽管在尝试执行此操作时遇到了null指针。我不完全确定这里出了什么问题,但希望有人能教我我做错了什么!

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

java.lang.NullPointerException网址:com.foxtrot.game.player.dialogues.impl.npcs.Child.run(Child.java:89(网址:com.foxtrot.game.player.DialogueManager.contineDialogue(DialogueManager.java:31(网址:com.foxtrot.net.dedecoders.WorldPacketsDecoder.processPackets(WorldPacketsDecider.java:1127(网址:com.foxtrot.net.dedecoders.WorldPacketsDecoder.decode(WorldPacketsDecider.java:282(网址:com.foxtrot.net.ServerChannelHandler.messageReceived(ServerChannelHandler.java:128(网址:org.jboss.nety.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88(网址:org.jboss.nety.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPiperine.java:564(网址:org.jboss.nety.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPiperine.java:559(网址:org.jboss.nety.channel.Channels.fireMessageReceived(Channels.java:268(网址:org.jboss.nety.channel.Channels.fireMessageReceived(Channels.java:255(网址:org.jboss.nety.channel.socket.nio.NioWorker.read(NioWorker.java:88(网址:org.jboss.nety.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108(网址:org.jboss.nety.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318(网址:org.jboss.nety.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89(网址:org.jboss.nety.channel.socket.nio.NioWorker.run(NioWorker.java:178(网址:org.jboss.nety.util.ThreadRenamingRunnable.run(ThreadRenamingRunable.java:108(网址:org.jboss.nety.util.internal.DedLockProofWorker$1.run(DeadLockProofWorker.java:42(位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源(位于java.util.concurrent.ThreadPoolExecutor$Worker.run(未知源(在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;
        }

CCD_ 1是第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方法就是从这里提取的。如果您需要更多信息,请告诉我!

谢谢。

if (player.getMonsterTask() == null)这意味着player.getMonsterTask((实际上返回了null
在下一行player.getMonsterTask().applyTask();中,实际上是在做null.applyTask()。因此出现了例外。

您正试图在null引用上调用一个方法。只有当方法是静态的时,这才有可能,否则将抛出NullPointerException

好吧,你问是否是monster.getTask()==null,然后调用monster.getTask().applyTask(),基本上是null.applyTask();(这就是NPE来自的地方

我认为您在设计中犯了一个错误。

如果从task枚举中删除private Player player;,并将player.getMonsterTask().applyTask();0更改为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();
}

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

相关内容

  • 没有找到相关文章

最新更新