1

Hello fellow coders,

I am writing a mod for Minecraft in 1.8 and have come across a pesky NullPointerException with my throwable. In the code below, the EntityThrowable uses an outer class to get the results of what will happen when thrown and the BlockPos passed is from the getBlockPos() method. This position is passed to the outer class where it transformed into x, y and z coords. However, whenever I throw the throwable, it throws an exception for these coordinates.

The difference between this question and the question of what is a NullPointerException is that the return value of what I am getting from the mov.getBlockPos() (from a MovingObjectPosition) is unknown. The MovingObjectPosition assigns the coords of the BlockPos from a random class and the coder of the Throwable gets the results. I am using the results for the outer class. These results in the ints cause the game to crash from unknown coords. If you have any idea of how to get the end pos of the throwable, that would be appreciated.

Here's the code:

Throwable:

@Override
protected void onImpact(MovingObjectPosition mov) {
    LuckyPotionItems lpi = new LuckyPotionItems();
    EntityPlayer player = this.getThrower() instanceof EntityPlayer ? (EntityPlayer) this.getThrower() : null;
    if(!worldObj.isRemote)
    lpi.chooseUnluckyDrink(worldObj, player, mov.getBlockPos());
    this.setDead();
}

Outer Class:

public void chooseUnluckyDrink(World w, EntityPlayer p, BlockPos pos){
    Random r = w.rand;
    int number = r.nextInt(13);
    int x = pos.getX();
    int y = pos.getY();
    int z = pos.getZ();

    System.out.println("Unlucky Number = " + number);

Thanks for any help.

  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – John3136 Apr 29 '16 at 01:49

2 Answers2

1

It sounds like the problem could be solved by checking if the BlockPos and the EntityPlayer are not null. If they aren't null, then run the method. If they are null, simply prevent the method from being ran.

@Override
protected void onImpact(MovingObjectPosition mov) {
    LuckyPotionItems lpi = new LuckyPotionItems();
    EntityPlayer player = this.getThrower() instanceof EntityPlayer ? (EntityPlayer) this.getThrower() : null;
    if(mov.getBlockPos() != null && player != null) {
        if(!worldObj.isRemote)
        lpi.chooseUnluckyDrink(worldObj, player, mov.getBlockPos());
        this.setDead();
    }
}
Flibio
  • 149
  • 12
  • Since the thrower will likely be an EntityPlayerMP or EntityPlayerSP (i don't have any entities that throw these throwables or throwable launcher), I doubt this will work but I will try. – TechGamer27 Apr 29 '16 at 02:23
  • 1
    @TechGamer27 I see, in that case, you also want to check if the `BlockPos` is null, I've updated my answer to include that. – Flibio Apr 29 '16 at 02:26
  • 1
    Was going to answer this :-) Always check your nulls, don't trust anything. sometimes code will be supplied by another mod, who lazily just supplies a null because then it works, or it's run on the server/client side and stuff turns to null too. Upvote to @Flibio – Tschallacka May 02 '16 at 10:00
0
number = (number == null) ? "" : number; 
System.out.println("Unlucky Number = " + number);

int x = (pos.getX() == null ) ? 0 :  pos.getX();

you are getting a null value for the number which means there is no output in r.nextInt(13); you need to fix that. Im using a conditional statement to check the value of number if its null then it will assign a value for number which can then be printed. try with my example this would help you.

Priyamal
  • 2,919
  • 2
  • 25
  • 52