Jump to content

[1.12.2] ObjectHolder Not Populating Fields


unassigned

Recommended Posts

Hello,

I am trying to register my blocks and items using the new ObjectHolder annotation, however, the fields that should be populated always return null. I've looked through the official documentation, however, I cannot find out what is wrong with my code.

Here is my ModBlocks and ModItems classes:

Spoiler

@GameRegistry.ObjectHolder(TestMod.MODID)
public class ModBlocks {

    public static final BlockTest testBlock = null;
}

@GameRegistry.ObjectHolder(TestMod.MODID)
public class ModItems {

    public static final ItemTest itemTest = null;
}

 

 

Here are the BlockBase, ItemBlockBase, and ItemBase classes:

Spoiler

public class BlockBase extends Block {

    public BlockBase(Material mat, String name){
        super(mat);
        this.setRegistryName(TestMod.MODID, name);
        this.setTranslationKey(getRegistryName().getPath());
    }
}

public class ItemBlockBase extends ItemBlock {

    public ItemBlockBase(Block block){
        super(block);
        this.setTranslationKey(block.getRegistryName().getPath());
        this.setRegistryName(block.getRegistryName());
        this.setCreativeTab(TestMod.creativeTab);
    }
}


public class ItemBase extends Item {

    public ItemBase(String name){
        this.setRegistryName(name);
        this.setTranslationKey(this.getRegistryName().getPath());
        this.setCreativeTab(TestMod.creativeTab);
    }
}

 

 

The test item and test block just extend these classes with no extra modifiers.

 

and lastly, here is my event subscriber:

Spoiler

@Mod.EventBusSubscriber(modid = TestMod.MODID)
public class EventSubscriber {

    @SubscribeEvent
    public static void onRegisterBlocks(final RegistryEvent.Register<Block> event){
        final IForgeRegistry<Block> registry = event.getRegistry();

        registry.register(new BlockTest("block_test"));

        TestMod.logger.info("Registered Blocks!");

        registerTileEntities();

        TestMod.logger.info("Registered Tile Entities!");
    }

    private static void regItemBlocks(IForgeRegistry<Item> registry){
        registry.register(new ItemBlockBase(ModBlocks.testBlock));    //this line crashes the game - as the testblock is still null.
    }

    @SubscribeEvent
    public static void onRegisterItems(final RegistryEvent.Register<Item> event){
        final IForgeRegistry<Item> registry = event.getRegistry();
        regItemBlocks(registry);

        registry.register(new ItemTest("item_test"));

        TestMod.logger.info("Registered Items!");
    }

    private static void registerTileEntities(){

    }

    private static void registerTileEntity(final Class<? extends TileEntity> clazz){
        try {
            GameRegistry.registerTileEntity(clazz, new ResourceLocation(TestMod.MODID, ModUtil.getRegistryNameForClass(clazz, "TileEntity")));
        } catch (final Exception e) {
            TestMod.logger.error("Error while attempting to register a tile entity!", e);
        }
    }
}

 

 

 

and for those interested, here is the error the game throws:

Spoiler

[21:42:21] [Client thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.RegistryEvent$Register@289bb421:
java.lang.NullPointerException: null
	at com.unassigned.testmod.block.base.ItemBlockBase.<init>(ItemBlockBase.java:20) ~[ItemBlockBase.class:?]
	at com.unassigned.testmod.EventSubscriber.regItemBlocks(EventSubscriber.java:46) ~[EventSubscriber.class:?]
	at com.unassigned.testmod.EventSubscriber.onRegisterItems(EventSubscriber.java:52) ~[EventSubscriber.class:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_4_EventSubscriber_onRegisterItems_Register.invoke(.dynamic) ~[?:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
	at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:144) ~[EventBus$1.class:?]
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) [EventBus.class:?]
	at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:782) [GameData.class:?]
	at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:628) [Loader.class:?]
	at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:252) [FMLClientHandler.class:?]
	at net.minecraft.client.Minecraft.init(Minecraft.java:514) [Minecraft.class:?]
	at net.minecraft.client.Minecraft.run(Minecraft.java:422) [Minecraft.class:?]
	at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_91]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_91]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
	at GradleStart.main(GradleStart.java:25) [start/:?]
[21:42:21] [Client thread/ERROR] [FML]: Index: 2 Listeners:
[21:42:21] [Client thread/ERROR] [FML]: 0: NORMAL
[21:42:21] [Client thread/ERROR] [FML]: 1: net.minecraftforge.fml.common.eventhandler.EventBus$1@3596a9d5
[21:42:21] [Client thread/ERROR] [FML]: 2: net.minecraftforge.fml.common.eventhandler.EventBus$1@441fd20d
[21:42:21] [Client thread/ERROR] [FML]: 3: net.minecraftforge.fml.common.eventhandler.EventBus$1@7146e9a4
[21:42:21] [Client thread/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: ---- Minecraft Crash Report ----
// This doesn't make any sense!

Time: 12/1/18 9:42 PM
Description: Initializing game

java.lang.NullPointerException: Initializing game
	at com.unassigned.testmod.block.base.ItemBlockBase.<init>(ItemBlockBase.java:20)
	at com.unassigned.testmod.EventSubscriber.regItemBlocks(EventSubscriber.java:46)
	at com.unassigned.testmod.EventSubscriber.onRegisterItems(EventSubscriber.java:52)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_4_EventSubscriber_onRegisterItems_Register.invoke(.dynamic)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
	at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:144)
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
	at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:782)
	at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:628)
	at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:252)
	at net.minecraft.client.Minecraft.init(Minecraft.java:514)
	at net.minecraft.client.Minecraft.run(Minecraft.java:422)
	at net.minecraft.client.main.Main.main(Main.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
	at GradleStart.main(GradleStart.java:25)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Client thread
Stacktrace:
	at com.unassigned.testmod.block.base.ItemBlockBase.<init>(ItemBlockBase.java:20)
	at com.unassigned.testmod.EventSubscriber.regItemBlocks(EventSubscriber.java:46)
	at com.unassigned.testmod.EventSubscriber.onRegisterItems(EventSubscriber.java:52)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_4_EventSubscriber_onRegisterItems_Register.invoke(.dynamic)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
	at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:144)
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
	at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:782)
	at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:628)
	at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:252)
	at net.minecraft.client.Minecraft.init(Minecraft.java:514)

-- Initialization --
Details:
Stacktrace:
	at net.minecraft.client.Minecraft.run(Minecraft.java:422)
	at net.minecraft.client.main.Main.main(Main.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
	at GradleStart.main(GradleStart.java:25)

 

 

Thank you.

Link to comment
Share on other sites

4 minutes ago, unassigned said:

I've looked through the official documentation

If you put the annotation on the class the field name must match the registry name. IE if the registry name is "block_test" the field name needs to be block_test or BLOCK_TEST.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

2 minutes ago, Animefan8888 said:

If you put the annotation on the class the field name must match the registry name. IE if the registry name is "block_test" the field name needs to be block_test or BLOCK_TEST.

Well, that was definitely the problem. Thanks.

Link to comment
Share on other sites

Don't use BlockBase or ItemBase or ItemBlockBase and all that

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.



×
×
  • Create New...

Important Information

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