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.
if (player.getMonsterTask() == null) { player.getMonsterTask().applyTask();You will always have aNullPointerExceptionwhen you reach this line, asplayer.getMonsterTask()will benull. - Florent Bayle