Posted June 16, 20169 yr Hi, I've got a huge problem when creating my own leaves block with a property enum which stores all types of leaves and woods. It always results into a NullPointerException and I don't know what's wrong. Here the crash: java.lang.NullPointerException: Initializing game at net.minecraft.block.state.BlockStateContainer.validateProperty(BlockStateContainer.java:101) at net.minecraft.block.state.BlockStateContainer.<init>(BlockStateContainer.java:75) at net.minecraft.block.state.BlockStateContainer.<init>(BlockStateContainer.java:60) at minecraftplaye.primevalforest.common.blocks.core.BlockBaseLeaves.createBlockState(BlockBaseLeaves.java:216) at net.minecraft.block.Block.<init>(Block.java:272) at net.minecraft.block.Block.<init>(Block.java:281) at net.minecraft.block.BlockLeaves.<init>(BlockLeaves.java:30) at minecraftplaye.primevalforest.common.blocks.core.BlockBaseLeaves.<init>(BlockBaseLeaves.java:49) at minecraftplaye.primevalforest.common.blocks.BlockLeaves.<init>(BlockLeaves.java:13) at minecraftplaye.primevalforest.common.core.ModBlocks.init(ModBlocks.java:30) at minecraftplaye.primevalforest.PrimevalForest.preinit(PrimevalForest.java:72) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:568) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) at com.google.common.eventbus.EventBus.post(EventBus.java:275) at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:228) at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:206) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) at com.google.common.eventbus.EventBus.post(EventBus.java:275) at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:135) at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:587) at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:249) at net.minecraft.client.Minecraft.startGame(Minecraft.java:475) at net.minecraft.client.Minecraft.run(Minecraft.java:384) at net.minecraft.client.main.Main.main(Main.java:118) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) at GradleStart.main(GradleStart.java:26) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Client thread Stacktrace: at net.minecraft.block.state.BlockStateContainer.validateProperty(BlockStateContainer.java:101) at net.minecraft.block.state.BlockStateContainer.<init>(BlockStateContainer.java:75) at net.minecraft.block.state.BlockStateContainer.<init>(BlockStateContainer.java:60) at minecraftplaye.primevalforest.common.blocks.core.BlockBaseLeaves.createBlockState(BlockBaseLeaves.java:216) at net.minecraft.block.Block.<init>(Block.java:272) at net.minecraft.block.Block.<init>(Block.java:281) at net.minecraft.block.BlockLeaves.<init>(BlockLeaves.java:30) at minecraftplaye.primevalforest.common.blocks.core.BlockBaseLeaves.<init>(BlockBaseLeaves.java:49) at minecraftplaye.primevalforest.common.blocks.BlockLeaves.<init>(BlockLeaves.java:13) at minecraftplaye.primevalforest.common.core.ModBlocks.init(ModBlocks.java:30) at minecraftplaye.primevalforest.PrimevalForest.preinit(PrimevalForest.java:72) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:568) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) at com.google.common.eventbus.EventBus.post(EventBus.java:275) at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:228) at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:206) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) at com.google.common.eventbus.EventBus.post(EventBus.java:275) at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:135) at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:587) at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:249) at net.minecraft.client.Minecraft.startGame(Minecraft.java:475) -- Initialization -- Details: Stacktrace: at net.minecraft.client.Minecraft.run(Minecraft.java:384) at net.minecraft.client.main.Main.main(Main.java:118) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) at GradleStart.main(GradleStart.java:26) Besides: I'm using Forge 1.9.4 with Java 8. BlockLeaves.java package minecraftplaye.primevalforest.common.blocks; import minecraftplaye.primevalforest.api.types.WoodType; import minecraftplaye.primevalforest.common.blocks.core.BlockBaseLeaves; import net.minecraft.block.properties.PropertyEnum; public class BlockLeaves extends BlockBaseLeaves { public static PropertyEnum<WoodType> META_PROPERTY = PropertyEnum.create("wood", WoodType.class/*, Arrays.copyOfRange(WoodType.values(), 0, 0)*/); public BlockLeaves() { super(META_PROPERTY); } } Block Base Leaves (the lines and methods marked in the crash log and the constructor): public abstract class BlockBaseLeaves extends BlockLeaves implements IShearable { protected final PropertyEnum<WoodType> META_PROP; public BlockBaseLeaves(PropertyEnum<WoodType> META_PROPERTY) { super(); this.META_PROP = META_PROPERTY; this.setLightOpacity(1); this.setTickRandomly(true); this.setTickRandomly(true); this.setSoundType(SoundType.PLANT); this.setCreativeTab(ModCreativeTabs.primevalTab); } The complete stuff is in the order of the crash log: @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {META_PROP}); } public BlockBaseLeaves(PropertyEnum<WoodType> META_PROPERTY) { super(); Here are all methods which have something to do with the meta data: @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {META_PROP}); } @Override public IBlockState getStateFromMeta(int meta) { return this.getDefaultState().withProperty(META_PROP, WoodType.getTypeFromID(meta)); } @Override public int getMetaFromState(IBlockState state) { return ((WoodType) state.getValue(META_PROP)).getID(); } @Override public EnumType getWoodType(int meta) { return null; } @Override public int damageDropped(IBlockState state) { return ((WoodType) state.getValue(META_PROP)).getID(); } My WoodType enum is currently filled with just 2 values: public enum WoodType implements IStringSerializable { // ID, name, Modulus of Rupture, Dried Weight Oak(0, "oak", 47.f), TEST(1, "test", 35.f); private final float driedWeight; private WoodType(int ID, String name, float driedWeight) { this.ID = ID; this.name = name; this.driedWeight = driedWeight / 10; } public static WoodType getTypeFromID(int ID) { for(int i = 0; i < WoodType.values().length; i++) { if(WoodType.values()[i].getID() == ID) return WoodType.values()[i]; } return null; } There are also getters for all values of the class. I hope someone can help me with this. Thx in advance. Bektor Developer of Primeval Forest.
June 16, 20169 yr Author Your property fields have to be static, otherwise they are not initialized yet when createBlockState is called, since createBlockState is called from the super constructor, but field initializers are run after that. So there is no way to just have one property field which will be set for each block? Because I don't like the idea of having to have them static, because when I have 32 wood types I need 2 classes and then I would need all the code in methods like updateTick() which can get large again.... and I'm not the fan of C&P code from class A to B. I mean, it was the main idea behind making the class abstract that I don't need to copy over my fast leave decay stuff to all classes because it requires the block metadata, so the property field to work. Developer of Primeval Forest.
June 16, 20169 yr If each subclass stores its property in a static field (and initialises it there), the base class can have an abstract method that returns the property and the subclasses can override it to return their property. You can call this method from createBlockState . BlockSlab does this for its variant property. Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
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.