Jump to content

Recommended Posts

Posted (edited)

Hello everyone!

I'd like to apologize in advance because I'm new to mod creation and my problem may be trivial. Minecraft crashes at startup and after some research, it seems that the problem comes from the ItemBlock Registration.
it seems that the `ItemBlock`s `block` variable is null and therefore when using `ItemBlock#getCreativeTab()` it throws an NPE.
However, I couldn't find the problem after several hours of searching (but there's a good chance it's a stupid mistake...).

Here is the repo : https://github.com/Dwight-Studio/Deepworld
(The awarded files will be directly quoted below).

 

Time: 8/30/20 2:50 PM
Description: Initializing game

java.lang.NullPointerException: Initializing game
	at net.minecraft.item.ItemBlock.getCreativeTab(ItemBlock.java:142)
	at net.minecraft.item.Item.getCreativeTabs(Item.java:691)
	at net.minecraft.item.Item.isInCreativeTab(Item.java:416)
	at net.minecraft.item.ItemBlock.getSubItems(ItemBlock.java:147)
	at net.minecraft.client.Minecraft.populateSearchTreeManager(Minecraft.java:587)
	at net.minecraft.client.Minecraft.init(Minecraft.java:529)
	at net.minecraft.client.Minecraft.run(Minecraft.java:378)
	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.legacydev.Main.start(Main.java:86)
	at net.minecraftforge.legacydev.MainClient.main(MainClient.java:29)

 

Or when I use ItemBlock#getBlock()

Time: 8/30/20 9:05 PM
Description: Initializing game

java.lang.NullPointerException: Initializing game
	at fr.dwightstudio.deepworld.common.DeepworldItems.prepareItemBlock(DeepworldItems.java:52)
	at fr.dwightstudio.deepworld.common.DeepworldItems.registerItemBlocks(DeepworldItems.java:39)
	at fr.dwightstudio.deepworld.common.Deepworld.registerItems(Deepworld.java:70)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_Deepworld_registerItems_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:852)
	at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:630)
	at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:252)
	at net.minecraft.client.Minecraft.init(Minecraft.java:467)
	at net.minecraft.client.Minecraft.run(Minecraft.java:378)
	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.legacydev.Main.start(Main.java:86)
	at net.minecraftforge.legacydev.MainClient.main(MainClient.java:29)

 

Deepworld.java

package fr.dwightstudio.deepworld.common;

import fr.dwightstudio.deepworld.common.tile.TileEntityWoodenFrame;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.oredict.OreDictionary;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(modid = Deepworld.MOD_ID)
@Mod.EventBusSubscriber()
public class Deepworld {

    // Mod info
    public static final String MOD_ID = "deepworld";
    public static final String MOD_NAME = "The Deep World";
    public static final String LOG_PREFIX = MOD_NAME;
    public static CreativeTabs creativeTab = new CreativeTabDeepworld();


    public static Logger logger = LogManager.getLogger(LOG_PREFIX);

    // This is the instance of your mod as created by Forge. It will never be null.
    @Mod.Instance(MOD_ID)
    public static Deepworld INSTANCE;

    /**
     * This is the first initialization event. Register tile entities here.
     * The registry events below will have fired prior to entry to this method.
     */
    @Mod.EventHandler
    public void preinit(FMLPreInitializationEvent event) {
        registerTileEntity();
    }

    // This is the second initialization event. Register custom recipes
    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {
        logger.info("Joining the stage...");
    }

    // This is the final initialization event. Register actions from other mods here
    @Mod.EventHandler
    public void postinit(FMLPostInitializationEvent event) {}

    // Blocks registration
    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {
        // Register blocks and tile entities
        logger.debug("Registering blocks...");
        DeepworldBlocks.registerBlocks(event.getRegistry());
        logger.debug("Done!");
    }

    // Items and itemBlocks registration
    @SubscribeEvent
    public static void registerItems(RegistryEvent.Register<Item> event) {
        // Items and itemBlocks registration
        logger.debug("Registering items...");
        DeepworldItems.registerItems(event.getRegistry());
        DeepworldItems.registerItemBlocks(event.getRegistry());

        // OreDictionnary registration
        OreDictionary.registerOre("frameWood", DeepworldItems.WOODEN_FRAME);
        OreDictionary.registerOre("frameIron", DeepworldItems.IRON_FRAME);
        OreDictionary.registerOre("frameSteel", DeepworldItems.STEEL_FRAME);
        OreDictionary.registerOre("frameObsidianSteel", DeepworldItems.OBSIDIAN_INFUSED_STEEL_FRAME);

        logger.debug("Done!");
    }

    // TileEntities registration
    public static void registerTileEntity() {
        logger.debug("Registering tileEntities...");
        GameRegistry.registerTileEntity(TileEntityWoodenFrame.class, new ResourceLocation(Deepworld.MOD_ID, "wooden_frame"));
        logger.debug("Done!");

    }
}

 

DeepworldItems.java

package fr.dwightstudio.deepworld.common;

import fr.dwightstudio.deepworld.common.item.ItemSimplePressingChamber;
import fr.dwightstudio.deepworld.common.item.ItemWoodenCasePanel;
import fr.dwightstudio.deepworld.common.item.ItemWoodenCrank;
import fr.dwightstudio.deepworld.common.item.ItemWoodenGearbox;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.registries.IForgeRegistry;

@GameRegistry.ObjectHolder(Deepworld.MOD_ID)
public class DeepworldItems {

    // Items initialization
    public static final Item WOODEN_CASE_PANEL = new ItemWoodenCasePanel();
    public static final Item SIMPLE_PRESSING_CHAMBER = new ItemSimplePressingChamber();
    public static final Item WOODEN_GEARBOX = new ItemWoodenGearbox();
    public static final Item WOODEN_CRANK = new ItemWoodenCrank();

    // Itemblocks initialization
    public static final Item WOODEN_FRAME = new ItemBlock(DeepworldBlocks.WOODEN_FRAME);
    public static final Item WOODEN_PRESS = new ItemBlock(DeepworldBlocks.WOODEN_PRESS);
    public static final Item IRON_FRAME = new ItemBlock(DeepworldBlocks.IRON_FRAME);
    public static final Item STEEL_FRAME = new ItemBlock(DeepworldBlocks.STEEL_FRAME);
    public static final Item OBSIDIAN_INFUSED_STEEL_FRAME = new ItemBlock(DeepworldBlocks.OBSIDIAN_INFUSED_STEEL_FRAME);

    // Items registration
    public static void registerItems(IForgeRegistry<Item> registry) {
        registry.register(prepare(WOODEN_CASE_PANEL, "wooden_case_panel"));
        registry.register(prepare(SIMPLE_PRESSING_CHAMBER, "simple_pressing_chamber"));
        registry.register(prepare(WOODEN_GEARBOX, "wooden_gearbox"));
        registry.register(prepare(WOODEN_CRANK, "wooden_crank"));
    }

    // Itemblocks
    public static void registerItemBlocks(IForgeRegistry<Item> registry) {
        registry.register(DeepworldItems.prepare(WOODEN_FRAME, "wooden_frame"));
        registry.register(DeepworldItems.prepare(WOODEN_PRESS, "wooden_press"));
        registry.register(DeepworldItems.prepare(IRON_FRAME, "iron_frame"));
        registry.register(DeepworldItems.prepare(STEEL_FRAME, "steel_frame"));
        registry.register(DeepworldItems.prepare(OBSIDIAN_INFUSED_STEEL_FRAME, "obsidian_infused_steel_frame"));
    }

    // Prepare items
    public static Item prepare(Item item, String name) {
        return item.setTranslationKey(name).setRegistryName(new ResourceLocation(Deepworld.MOD_ID, name));
    }
}

 

DeepworldBlocks.java

package fr.dwightstudio.deepworld.common;

import fr.dwightstudio.deepworld.common.block.*;
import net.minecraft.block.Block;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.registries.IForgeRegistry;

@GameRegistry.ObjectHolder(Deepworld.MOD_ID)
public class DeepworldBlocks {

    // Blocks initialization
    public static final Block WOODEN_FRAME = new BlockWoodenFrame();
    public static final Block WOODEN_PRESS =  new BlockWoodenPress();
    public static final Block IRON_FRAME = new BlockIronFrame();
    public static final Block STEEL_FRAME = new BlockSteelFrame();
    public static final Block OBSIDIAN_INFUSED_STEEL_FRAME = new BlockObsidianInfusedSteelFrame();

    // Blocks registration
    public static void registerBlocks(IForgeRegistry<Block> registry) {
        registry.register(prepareBlock(WOODEN_FRAME, "wooden_frame"));
        registry.register(prepareBlock(WOODEN_PRESS, "wooden_press"));
        registry.register(prepareBlock(IRON_FRAME, "iron_frame"));
        registry.register(prepareBlock(STEEL_FRAME, "steel_frame"));
        registry.register(prepareBlock(OBSIDIAN_INFUSED_STEEL_FRAME, "obsidian_infused_steel_frame"));
    }

    // Prepare block
    public static Block prepareBlock(Block block, String name) {
        return block.setTranslationKey(name).setRegistryName(new ResourceLocation(Deepworld.MOD_ID, name));
    }
}

 

Thank you in advance!

Edited by Deleranax
Syndax
Posted (edited)

1.12 isn't supported, didn't see that, update to at least 1.14

 

https://github.com/Dwight-Studio/Deepworld/blob/1.12.2/src/main/java/fr/dwightstudio/deepworld/common/DeepworldItems.java#L23

 

Problematic Code #14

Create your items properly.

 

Other things:

https://github.com/Dwight-Studio/Deepworld/blob/1.12.2/src/main/java/fr/dwightstudio/deepworld/common/DeepworldItems.java#L39

You don't need the DeepWorld. here, the prepare method is already available in the current scope.

 

https://github.com/Dwight-Studio/Deepworld/blob/1.12.2/src/main/java/fr/dwightstudio/deepworld/common/Deepworld.java#L46-L62

Do not log random garbage. No one fucking cares.

 

https://github.com/Dwight-Studio/Deepworld/blob/1.12.2/src/main/java/fr/dwightstudio/deepworld/common/block/BlockFrame.java#L30

If a type of property already exists in vanilla, it is preferential to use the existing property rather than creating a new one. In this case BlockHorizontal.FACING, as it makes intermod compatibility cleaner (eg. an item that can rotate FACING blocks can't rotate your block).

 

https://github.com/Dwight-Studio/Deepworld/blob/1.12.2/src/main/java/fr/dwightstudio/deepworld/common/block/BlockFrame.java#L141-L144

Don't override unless you intend to actually alter functionality.

 

https://github.com/Dwight-Studio/Deepworld/blob/1.12.2/src/main/java/fr/dwightstudio/deepworld/common/item/ItemDeepworld.java

Do not create "base" items that your mod items extend.

Code Style #4

 

 

Edited by Draco18s
  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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