Posted April 24, 201510 yr I'm getting a null pointer exception in this function: public void processCommand(ICommandSender sender, String[] args) { if(args.length == 0){ PlayerWallet wallet = PlayerWallet.get((EntityPlayer) sender); ((EntityPlayer) sender).addChatComponentMessage(new ChatComponentText("£" + wallet.getWallet())); } i know the error occurs at "wallet.getWallet()". i just can't work out what is causing the error suggestions? the Player wallet class: public class PlayerWallet implements IExtendedEntityProperties { public final static String EXT_PROP_NAME = "PlayerWallet"; private double funds; public void init(Entity entity, World world){ funds = 0; } public void loadNBTData(NBTTagCompound nbt){ NBTTagCompound properties = (NBTTagCompound) nbt.getTag(EXT_PROP_NAME); properties.getDouble("funds"); } public void saveNBTData(NBTTagCompound nbt){ NBTTagCompound properties = new NBTTagCompound(); properties.setDouble("funds", funds); nbt.setTag(EXT_PROP_NAME, properties); } public static final PlayerWallet get(EntityPlayer player) { return (PlayerWallet) player.getExtendedProperties(EXT_PROP_NAME); } public static final void register(EntityPlayer player) { player.registerExtendedProperties(PlayerWallet.EXT_PROP_NAME, new PlayerWallet()); } public double getWallet(){ return funds; } public double addFunds(double f){ funds += f; return getWallet(); } public double setFunds(double f){ funds = f; return funds; } public double removeFunds(double f){ funds -=f; return funds; } } The Event handler class: public class MconomyEventHandler { @EventHandler public void onEntityConstructing(EntityEvent.EntityConstructing event) { /* Be sure to check if the entity being constructed is the correct type for the extended properties you're about to add! The null check may not be necessary - I only use it to make sure properties are only registered once per entity */ if (event.entity instanceof EntityPlayer && PlayerWallet.get((EntityPlayer) event.entity) == null) // This is how extended properties are registered using our convenient method from earlier PlayerWallet.register((EntityPlayer) event.entity); // That will call the constructor as well as cause the init() method // to be called automatically } } the event is registered here: @EventHandler public void init(FMLInitializationEvent event) { // some example code MinecraftForge.EVENT_BUS.register(new MconomyEventHandler()); } i would really appreciate if anyone can solve this problem for me
April 25, 201510 yr If your IEEP class is returning null, chances are you did not register it correctly during EntityConstructing event, or you did not register your event handler class. Show your event handling class and registration. http://i.imgur.com/NdrFdld.png[/img]
April 25, 201510 yr I added those 2 bits of code to the end of the first post @EventHandler is only for the FML events involved in the mod loading process - all other events use @SubscribeEvent. Next time something doesn't seem to be working, put a println statement inside and watch your console - if it doesn't print, you then have more knowledge of what the problem might be. E.g. @EventHandler public void onEntityConstructing(EntityConstructing event) { System.out.println("Entity is constructing."); // Doesn't print, so you know the problem has to do with your event } Also, for code, either use [ code ]code here[ / code ] tags (without spaces) or a service like pastebin - it makes it far more readable. http://i.imgur.com/NdrFdld.png[/img]
April 25, 201510 yr Author oh so that's how i format code haha... i tried using the button at the top of the textbox (picture of a hash tag) but it didnt do anything lol. thanks
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.