1

Nullpointer on item class

QuickScoP3s's Avatar QuickScoP3s4/21/15 2:28 pm
1 emeralds 229 2
4/22/2015 11:33 am
QuickScoP3s's Avatar QuickScoP3s
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.
@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
Posted by QuickScoP3s's Avatar
QuickScoP3s
Level 55 : Grandmaster Programmer
26

Create an account or sign in to comment.

2

1
04/22/2015 11:33 am
Level 55 : Grandmaster Programmer
QuickScoP3s
QuickScoP3s's Avatar
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
1
04/21/2015 2:34 pm
Level 88 : Elite Scapegoat
Paril
Paril's Avatar
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.
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome