Jump to content

[1.15.2] Capability is null


xanderindalzone

Recommended Posts

I'm getting a null error when I try to get the player's custom capability, I dont know why.... I must have missed something but I dont know where.... if someone could tell where's the problem i would be grateful.

 

Quote

java.lang.NullPointerException: null
    at com.xanderindalzone.customgunsmod.events.client.ClientKeyEvents.AimGun(ClientKeyEvents.java:92) ~[main/:?] {re:classloading}
    at net.minecraftforge.eventbus.ASMEventHandler_8_ClientKeyEvents_AimGun_ClientTickEvent.invoke(.dynamic) ~[?:?] {}
    at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:80) ~[eventbus-2.2.0-service.jar:?] {}
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258) ~[eventbus-2.2.0-service.jar:?] {}
    at net.minecraftforge.fml.hooks.BasicEventHooks.onPreClientTick(BasicEventHooks.java:104) ~[forge-1.15.2-31.2.0_mapped_snapshot_20200514-1.15.1-recomp.jar:?] {re:classloading}
    at net.minecraft.client.Minecraft.runTick(Minecraft.java:1387) ~[forge-1.15.2-31.2.0_mapped_snapshot_20200514-1.15.1-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:941) ~[forge-1.15.2-31.2.0_mapped_snapshot_20200514-1.15.1-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.run(Minecraft.java:558) ~[forge-1.15.2-31.2.0_mapped_snapshot_20200514-1.15.1-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.main.Main.main(Main.java:177) ~[forge-1.15.2-31.2.0_mapped_snapshot_20200514-1.15.1-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251] {}
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251] {}
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251] {}
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251] {}
    at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:55) ~[forge-1.15.2-31.2.0_mapped_snapshot_20200514-1.15.1-recomp.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-5.1.0.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-5.1.0.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-5.1.0.jar:?] {}
    at cpw.mods.modlauncher.Launcher.run(Launcher.java:81) [modlauncher-5.1.0.jar:?] {}
    at cpw.mods.modlauncher.Launcher.main(Launcher.java:65) [modlauncher-5.1.0.jar:?] {}
    at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:102) [forge-1.15.2-31.2.0_mapped_snapshot_20200514-1.15.1-recomp.jar:?] {}

 

Event Handler where I call the capability

Quote

 

Link to comment
Share on other sites

Do something like this for AimGun:

Change event to TickEvent.PlayerTickEvent

Use event.player to get player

and get capability like this:

@SubscribeEvent
public static void tick(TickEvent.PlayerTickEvent event) {
   [YourIinterface] data = event.player.getCapability(ProviderGunActions.GUN_ACTIONS_CAP).orElseThrow(IllegalStateException::new);
    
}
Link to comment
Share on other sites

That's not the problem. That's just a different way to access a capability and ifPresent is perfectly acceptable (and indeed, preferred).

 

I suspect the issue is that ClientTickEvent runs in the main menu before there is a world. Before there is a player and there is no null-check on line 92:

 

Minecraft.getInstance().player.[...]

 

player, there, may be null!

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.

Link to comment
Share on other sites

Now I'm getting this null exception when I join the world :V (CODE UPDATED on Github)

Quote

java.lang.NullPointerException
    at com.xanderindalzone.customgunsmod.capabilities.providers.ProviderGunActions.<init>(ProviderGunActions.java:19)
    at com.xanderindalzone.customgunsmod.events.GunCapabilityHandler.attachCapability(GunCapabilityHandler.java:71)
    at net.minecraftforge.eventbus.ASMEventHandler_10_GunCapabilityHandler_attachCapability_AttachCapabilitiesEvent.invoke(.dynamic)
    at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:80)
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258)
    at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:564)
    at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:558)
    at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:48)
    at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:44)
    at net.minecraft.entity.Entity.<init>(Entity.java:224)
    at net.minecraft.entity.LivingEntity.<init>(LivingEntity.java:200)
    at net.minecraft.entity.player.PlayerEntity.<init>(PlayerEntity.java:164)
    at net.minecraft.entity.player.ServerPlayerEntity.<init>(ServerPlayerEntity.java:161)
    at net.minecraft.server.management.PlayerList.createPlayerForUser(PlayerList.java:395)
    at net.minecraft.network.login.ServerLoginNetHandler.tryAcceptPlayer(ServerLoginNetHandler.java:120)
    at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:64)
    at net.minecraft.network.NetworkManager.tick(NetworkManager.java:246)
    at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:152)
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:903)
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:821)
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:662)
    at java.lang.Thread.run(Unknown Source)
 

 

 

Isnt this the way it has to be implemented?

Quote

    @CapabilityInject(IGunActions.class)
    public static final Capability<IGunActions> GUN_ACTIONS_CAP = null;

    private LazyOptional<IGunActions> instance = LazyOptional.of(GUN_ACTIONS_CAP::getDefaultInstance);

 

PD: I think I got this line from another post.

Link to comment
Share on other sites

29 minutes ago, diesieben07 said:

This method is never called (you don't register it as an event handler) and thus your capability is never registered. Therefor your @CapabilityInject field stays null.

Oh yeah, I'm dumb, I copypasted the code and forgot to change the method name in the parameter :P, thx XD

I sometimes change code too fast that I dont realize of these silly mistakes XD

Its now joining the world without crashes :) thx

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I add a search bar to CreativeModeTab, the text box is not rendered even though it's possible to input text. Additionally, the search is performed on all items. This issue has been occurring since 1.21. Is there something missing in my code? https://github.com/mgen256/AdditionalLights/blob/1.21/src/main/java/com/mgen256/al/AdditionalLights.java
    • This question would probably best be asked wherever fabric support is given; these are the Minecraft Forge forums. They aren't the same thing
    • client version: forge-1.7.10-10.13.4.1614-1.7.10 forgegradle version:forge-1.7.10-10.13.4.1614-1.7.10   i can run my mod in forgegradle but i put my mod into a client ,it will crash  cpw.mods.fml.common.LoaderException: java.lang.NoSuchMethodError: com.example.examplemod.items.CommonItem.setUnlocalizedName(Ljava/lang/String;)Lnet/minecraft/item/Item;     at cpw.mods.fml.common.LoadController.transition(LoadController.java:163)     at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:559)     at cpw.mods.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:243)     at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:480)     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:878)     at net.minecraft.client.main.Main.main(SourceFile:148)     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:497)     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:497)     at oolloo.jlw.Wrapper.invokeMain(Wrapper.java:71)     at oolloo.jlw.Wrapper.main(Wrapper.java:51) Caused by: java.lang.NoSuchMethodError: com.example.examplemod.items.CommonItem.setUnlocalizedName(Ljava/lang/String;)Lnet/minecraft/item/Item;     at com.example.examplemod.items.CommonItem.<init>(CommonItem.java:10)     at com.example.examplemod.proxy.register.ItemRegister.createItem(ItemRegister.java:30)     at com.example.examplemod.proxy.register.ItemRegister.registerItems(ItemRegister.java:18)     at com.example.examplemod.proxy.CommonProxy.preInit(CommonProxy.java:12)     at com.example.examplemod.proxy.ClientProxy.preInit(ClientProxy.java:6)     at com.example.examplemod.ExampleMod.preInit(ExampleMod.java:26)     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:497)     at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:532)     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     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 cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:212)     at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:190)     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:497)     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 cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:119)     at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:556)     ... 16 more  
  • Topics

×
×
  • Create New...

Important Information

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