Jump to content

NullPointerException - Ticks - getHeldItem()


BadBoy6767

Recommended Posts

Hey everyone, i have a NullPointerException problem, i know what it is an all since i experienced java for 4 years,

though, i have no idea how to avoid it since i rarely had this problem,

im trying to check the item held by the player in a tick event.

 

This is the main file:

 

 

package com.lvivtotoro.forgemod.interruptuswandus;

 

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.init.Blocks;

import net.minecraft.init.Items;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import cpw.mods.fml.common.FMLCommonHandler;

import cpw.mods.fml.common.Mod;

import cpw.mods.fml.common.Mod.EventHandler;

import cpw.mods.fml.common.event.FMLPreInitializationEvent;

import cpw.mods.fml.common.registry.GameRegistry;

import cpw.mods.fml.common.registry.LanguageRegistry;

 

@Mod(modid = InterruptusWandus.MODID, version = InterruptusWandus.VERSION)

public class InterruptusWandus {

 

public static final String MODID = "interruptuswandus";

public static final String VERSION = "0.1";

 

public static final Item emptyWand = new Item().setUnlocalizedName(

"wandEmpty").setTextureName(MODID + ":emptyWand");

public static final Item fireWand = new ItemFireWand().setUnlocalizedName(

"wandFire").setTextureName(MODID + ":fireWand");

public static final Item iceWand = new ItemIceWand().setUnlocalizedName(

"wandIce").setTextureName(MODID + ":iceWand");

public static final Item lightningWand = new ItemLightningWand()

.setUnlocalizedName("wandEl").setTextureName(MODID + ":elWand");

 

@EventHandler

public void preInit(FMLPreInitializationEvent e) {

FMLCommonHandler.instance().bus().register(new LightningWandTickHandler());

GameRegistry.registerItem(emptyWand, "iwEmptyWand");

emptyWand.setCreativeTab(CreativeTabs.tabTools);

LanguageRegistry.addName(emptyWand, "Empty Wand");

GameRegistry.addRecipe(new ItemStack(emptyWand, 1), new Object[] {

" G", "S ", 'G', Blocks.glass, 'S', Items.stick });

GameRegistry.registerItem(fireWand, "iwFireWand");

fireWand.setCreativeTab(CreativeTabs.tabTools);

LanguageRegistry.addName(fireWand, "§cFire Wand");

GameRegistry.addRecipe(new ItemStack(fireWand, 1), new Object[] {

"FSF", "DWD", "III", 'F', Items.flint, 'S',

Items.flint_and_steel, 'I', Items.iron_ingot, 'W', emptyWand });

GameRegistry.registerItem(iceWand, "iwIceWand");

iceWand.setCreativeTab(CreativeTabs.tabTools);

LanguageRegistry.addName(iceWand, "§3Ice Wand");

GameRegistry.addRecipe(new ItemStack(iceWand, 1), new Object[] { "FFF",

" W ", "III", 'F', Blocks.ice, 'I', Items.diamond, 'W',

emptyWand });

GameRegistry.registerItem(lightningWand, "iwLightningWand");

lightningWand.setCreativeTab(CreativeTabs.tabTools);

LanguageRegistry.addName(lightningWand, "§5Lightning Wand");

GameRegistry.addRecipe(new ItemStack(lightningWand, 1), new Object[] { "FFF",

" W ", "FFF", 'F', Items.glowstone_dust,

'W', emptyWand });

}

 

}

 

 

 

This is the event:

 

 

package com.lvivtotoro.forgemod.interruptuswandus;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.entity.EntityClientPlayerMP;

import net.minecraft.item.Item;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent;

 

public class LightningWandTickHandler {

 

@SubscribeEvent

public void onUpdate(ClientTickEvent event) {

EntityClientPlayerMP p = Minecraft.getMinecraft().thePlayer;

if(InterruptusWandus.lightningWand.equals(p.getHeldItem())) {

if(!p.capabilities.isCreativeMode) {

p.addPotionEffect(new PotionEffect(Potion.moveSlowdown.id, 1, 1));

p.addPotionEffect(new PotionEffect(Potion.damageBoost.id, 1, 1));

}

}

}

 

}

 

 

Link to comment
Share on other sites

Either this is being called before the player actually spawns, or the players held item is null.

 

 

Also, why aren't you using the onUpdate method in your custom item class? It gets called whenever the item is in the players inventory which means you could do your check from there.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

And from your "60% worthless" post right there I assume you just copy/pasted the exact same code you had in the event handler. So of course its not going to work. As diesieben07 kindly told you:

a) This won't work, you can't add PotionEffects on the client.

 

So you need to use the parameters given in the method, but first surround it all with a check to see if the world is server side world.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.