Jump to content

Registry Object not present when registering BlockItem.


Darkorg69

Recommended Posts

So I am getting this error while trying to register a seed blockitem for a crop block.

Spoiler
[18:51:15] [modloading-worker-0/ERROR] [ne.mi.fm.ja.FMLModContainer/LOADING]: Failed to create mod instance. ModID: bettercrops, class darkorg.bettercrops.BetterCrops
java.lang.reflect.InvocationTargetException: null
	at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?] {}
	at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[?:?] {}
	at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?] {}
	at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[?:?] {}
	at java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[?:?] {}
	at net.minecraftforge.fml.javafmlmod.FMLModContainer.constructMod(FMLModContainer.java:67) ~[javafmllanguage-1.18.2-40.1.86.jar%2380!/:?] {}
	at net.minecraftforge.fml.ModContainer.lambda$buildTransitionHandler$4(ModContainer.java:106) ~[fmlcore-1.18.2-40.1.86.jar%2383!/:?] {}
	at java.util.concurrent.CompletableFuture$AsyncRun.run$$$capture(CompletableFuture.java:1804) [?:?] {}
	at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java) [?:?] {}
	at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796) [?:?] {}
	at java.util.concurrent.ForkJoinTask.doExec$$$capture(ForkJoinTask.java:373) [?:?] {}
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java) [?:?] {}
	at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) [?:?] {}
	at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) [?:?] {}
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) [?:?] {}
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) [?:?] {}
Caused by: java.lang.ExceptionInInitializerError
	at darkorg.bettercrops.BetterCrops.<init>(BetterCrops.java:22) ~[%2384!/:?] {re:classloading}
	... 16 more
Caused by: java.lang.NullPointerException: Registry Object not present: bettercrops:barley
	at java.util.Objects.requireNonNull(Objects.java:334) ~[?:?] {}
	at net.minecraftforge.registries.RegistryObject.get(RegistryObject.java:320) ~[forge-1.18.2-40.1.86_mapped_parchment_2022.11.06-1.18.2-recomp.jar%2378%2385!/:?] {re:classloading,xf:fml:forge:registry_object_binary_compat}
	at darkorg.bettercrops.registry.ModItems.<clinit>(ModItems.java:17) ~[%2384!/:?] {re:classloading}
	at darkorg.bettercrops.BetterCrops.<init>(BetterCrops.java:22) ~[%2384!/:?] {re:classloading}
	... 16 more
[18:51:16] [main/FATAL] [ne.mi.fm.ModLoader/LOADING]: Failed to complete lifecycle event CONSTRUCT, 1 errors found
Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:39)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.Launcher.run(Launcher.java:106)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.Launcher.main(Launcher.java:77)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23)
	at [email protected]/cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:149)
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at MC-BOOTSTRAP/[email protected]/net.minecraftforge.fml.loading.targets.ForgeDataUserdevLaunchHandler.lambda$launchService$0(ForgeDataUserdevLaunchHandler.java:27)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37)
	... 7 more
Caused by: net.minecraftforge.fml.LoadingFailedException: Loading errors encountered: [
	fml.modloading.failedtoloadmod
]
	at LAYER PLUGIN/[email protected]/net.minecraftforge.fml.ModLoader.waitForTransition(ModLoader.java:222)
	at LAYER PLUGIN/[email protected]/net.minecraftforge.fml.ModLoader.lambda$dispatchAndHandleError$21(ModLoader.java:187)
	at java.base/java.util.Optional.ifPresent(Optional.java:178)
	at LAYER PLUGIN/[email protected]/net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:187)
	at LAYER PLUGIN/[email protected]/net.minecraftforge.fml.ModLoader.lambda$gatherAndInitializeMods$11(ModLoader.java:164)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at LAYER PLUGIN/[email protected]/net.minecraftforge.fml.ModLoader.gatherAndInitializeMods(ModLoader.java:164)
	at TRANSFORMER/[email protected]/net.minecraftforge.data.loading.DatagenModLoader.begin(DatagenModLoader.java:38)
	at TRANSFORMER/[email protected]/net.minecraft.data.Main.main(Main.java:67)
	... 13 more
Disconnected from the target VM, address: '127.0.0.1:54565', transport: 'socket'

Process finished with exit code 1

 

Blocks class:

Spoiler
public class ModBlocks {
    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, BetterCrops.MOD_ID);

    public static final RegistryObject<Block> BARLEY = registerCrop("barley");

    private static RegistryObject<Block> registerCrop(String pName) {
        return BLOCKS.register(pName, ModCropBlock::new);
    }
}

 

Items class:

Spoiler
public class ModItems {
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, BetterCrops.MOD_ID);
    
    public static final RegistryObject<Item> BARLEY_SEEDS = registerSeed("barley_seeds", ModBlocks.BARLEY.get());

    private static RegistryObject<Item> registerSeed(String pSeed, Block pCrop) {
        return ITEMS.register(pSeed, () -> new SeedBlockItem(pCrop));
    }
}

 

SeedBlockItem class:

Spoiler
public class SeedBlockItem extends ItemNameBlockItem {
    public SeedBlockItem(Block pBlock) {
        super(pBlock, new Properties().tab(ModCreativeTabs.TAB_SEEDS));
    }
}

 

ModCropBlock class:

Spoiler
public class ModCropBlock extends CropBlock {
    public ModCropBlock() {
        super(BlockBehaviour.Properties.of(Material.PLANT).noCollission().randomTicks().instabreak().sound(SoundType.CROP));
    }

    @Override
    protected @NotNull ItemLike getBaseSeedId() {
        return this.asItem();
    }
}

 

Main class:

Spoiler
@Mod(BetterCrops.MOD_ID)
public class BetterCrops {
    public static final String MOD_ID = "bettercrops";

    IEventBus forgeBus = MinecraftForge.EVENT_BUS;

    IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus();

    public BetterCrops() {
        ServerConfig.init();
        ModBlocks.BLOCKS.register(modBus);
        ModItems.ITEMS.register(modBus);
        forgeBus.register(this);
    }
}

 

I have registered the block but the supplier.get() doesn't resolve the block for some reason. Any ideas what is wrong?

Link to comment
Share on other sites

You are trying to access the unregistered block during classloading (static initialisation).

You need to understand how deferred references work (Suppliers, RegistryObject, etc.) and when you can call get()

This is an important pattern within forge/minecraft.

 

Something like:

 
    // Pass the supplier, don't try to get the unregistered block yet
    public static final RegistryObject<Item> BARLEY_SEEDS = registerSeed("barley_seeds", ModBlocks.BARLEY);

    // Method accepts the supplier
    private static RegistryObject<Item> registerSeed(String pSeed, RegistryObject<Block> pCrop) {
        // Retrieve the block object at registration time
        return ITEMS.register(pSeed, () -> new SeedBlockItem(pCrop.get()));
    }

 

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.