1
Nullpointer on item class
hi there,
I have run into some problems while making my mod:
As the title suggests, it's a nullpointer:
the situation is within an eventhandler.
As you can see in my code, the error is on the this and my question is why, because the class is extended item:
public class SomeItem extends Item
If someone would help me, so I can finish my project and upload it (I'm updating my CastleDefendersMod: http://www.planetminecraft.com/mod/the-castledefendersmod/)
Thanks in advance and i'm definitly gonna leave your name in the special thanks
Kind regards,
QuickScoP3s
I have run into some problems while making my mod:
As the title suggests, it's a nullpointer:
the situation is within an eventhandler.
@SubscribeEvent
public void entityDamage(AttackEntityEvent event) {
Entity target = event.target;
if (target instanceof EntityPlayer) {
EntityPlayer targetPlayer = (EntityPlayer) target;
if (targetPlayer.getItemInUse().getItem() == this) { //Error NullPointer on "This"
if (this.isBlocking) {
event.setCanceled(true);
}
}
}
}
@SubscribeEvent
public void entityDamage(LivingHurtEvent event) {
Entity target = event.entity;
if (target instanceof EntityPlayer) {
EntityPlayer targetPlayer = (EntityPlayer) target;
if (targetPlayer.getItemInUse().getItem() == this) { //Error NullPointer on "This"
if (this.isBlocking) {
event.setCanceled(true);
}
}
}
}
As you can see in my code, the error is on the this and my question is why, because the class is extended item:
public class SomeItem extends Item
If someone would help me, so I can finish my project and upload it (I'm updating my CastleDefendersMod: http://www.planetminecraft.com/mod/the-castledefendersmod/)
Thanks in advance and i'm definitly gonna leave your name in the special thanks
Kind regards,
QuickScoP3s
Create an account or sign in to comment.
2
1
Thank you very much, a PMC developer that helped me out, what an honor!
I know, it was a 'stupid' mistake, forgetting to null-check
I know, it was a 'stupid' mistake, forgetting to null-check
1
The null pointer is likely on this chain: "targetPlayer.getItemInUse().getItem()"
If the target player doesn't have an item in use, that will return null - and you can't "getItem()" on a null item. You will have to be sure that targetPlayer.getItemInUse() is not null.
If the target player doesn't have an item in use, that will return null - and you can't "getItem()" on a null item. You will have to be sure that targetPlayer.getItemInUse() is not null.