Jump to content

[1.15.1] capability NullPointerException


transfarmer

Recommended Posts

Hello. I made a custom capability that should be attached to EntityPlayer. My provider class initializes an instance of Capabilty and sets its value to null and makes a LazyOptional instance of this capability:

    public static final Capability<ISoulWeapon> TYPE = null;
    private LazyOptional<ISoulWeapon> instance = LazyOptional.of(TYPE::getDefaultInstance);

I assume that this is correct because this is the only way in which I saw this done. However, the instance seems to be null. The game runs until a world is loaded, when it crashes, producing the following stacktrace:

java.lang.NullPointerException: Saving entity NBT
	at transfarmer.adventureitems.capabilities.Storage.writeNBT(Storage.java:14) ~[?:?] {re:classloading}
	at transfarmer.adventureitems.capabilities.Storage.writeNBT(Storage.java:10) ~[?:?] {re:classloading}
	at transfarmer.adventureitems.capabilities.Provider.serializeNBT(Provider.java:23) ~[?:?] {re:classloading,pl:capability_inject_definalize:A}
	at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:127) ~[?:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.serializeCaps(CapabilityProvider.java:86) ~[?:?] {re:classloading}
	at net.minecraft.entity.Entity.writeWithoutTypeId(Entity.java:1527) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.integrated.IntegratedPlayerList.writePlayerData(IntegratedPlayerList.java:27) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.management.PlayerList.saveAllPlayerData(PlayerList.java:631) ~[?:?] {re:classloading}
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:112) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:658) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_232] {}

What did I do wrong?

My capability is available here.

Link to comment
Share on other sites

Well, you set it to null and made it final. It can't be anything else (unless you apply the @ObjectHolder annotation or use reflection).

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

I read again the Forge documentation about capabilities. According to the it, the capability is null because it was not registered. Then I made it non-final and tried to properly register it during common setup. The NullPointerException does not occur anymore. However, my keybinding does not load anymore (I set its registration to occur during client setup by FMLClientSetupEvent as I was cleaning up my mod setup). Previously, it loaded but did not function. (It functioned as expected before I implemented the capability.) Should I open a new thread for that?

Also, the 1.15.x documentation mentions a provider hasCapability method. Is that a requirement in previous versions of Forge? It is not defined in ICapabilityProvider.java in v30.0.36.

Edited by transfarmer
Link to comment
Share on other sites

11 minutes ago, transfarmer said:

Is that a requirement in previous versions of Forge?

Yes. getCapability is pretty much the only thing that interface provides.

  • Thanks 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.

Link to comment
Share on other sites

The Forge documentation states the following:

Quote

The return type of getCapability will correspond to the type declared in the capability passed to the method. For the Item Handler capability, this is indeed IItemHandler.

However, the return type of getCapability is LazyOptional<T>, and I want to use the methods defined in my interface. How should I use getCapability?

Link to comment
Share on other sites

Yes. It should be LazyOptional<T>. Note that <T> here is still the type of the capability, so you'll get back a LazyOptional<IItemHandler>. Which you can then call ifPresent() on and pass in a lambda that gets executed if the capability is indeed present (non-null) or you can do something like OrElse(null) and null check (prefer using ifPresent) or use OrElseThrow().

  • Thanks 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.

Link to comment
Share on other sites

You need to attach capabilities to things you don't control. Like the player. Use the EntityJoinedWorldEvent for that.

  • Thanks 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.

Link to comment
Share on other sites

4 hours ago, diesieben07 said:

AttachCapabilitiesEvent

I could not remember if this was actually a thing.

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

I already have it:

@EventBusSubscriber(modid = Main.MODID, bus = EventBusSubscriber.Bus.MOD)
public class ModEventSubscriber {
    . . .
    @SubscribeEvent
    public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) {
        if (event.getObject() instanceof PlayerEntity) {
            event.addCapability(new ResourceLocation(Main.MODID, "type"), new Provider());
        }
    }
    . . .
}

Is something wrong with this code?

Edited by transfarmer
Link to comment
Share on other sites

Loading a world produces a NullPointerExceptionagain. The ICapability instance passed to writeNBT seems to be null:

Spoiler

java.lang.NullPointerException: Saving entity NBT
    at transfarmer.adventureitems.capability.SoulWeaponStorage.writeNBT(SoulWeaponStorage.java:14) ~[?:?] {re:classloading}
    at transfarmer.adventureitems.capability.SoulWeaponStorage.writeNBT(SoulWeaponStorage.java:10) ~[?:?] {re:classloading}
    at transfarmer.adventureitems.capability.SoulWeaponProvider.serializeNBT(SoulWeaponProvider.java:26) ~[?:?] {re:classloading}
    at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:127) ~[?:?] {re:classloading}
    at net.minecraftforge.common.capabilities.CapabilityProvider.serializeCaps(CapabilityProvider.java:86) ~[?:?] {re:classloading}
    at net.minecraft.entity.Entity.writeWithoutTypeId(Entity.java:1358) ~[?:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.integrated.IntegratedPlayerList.writePlayerData(SourceFile:24) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.server.management.PlayerList.saveAllPlayerData(PlayerList.java:602) ~[?:?] {re:classloading}
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:106) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:634) ~[?:?] {re:classloading,pl:accesstransformer:B}
    at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_232] {}

 

Spoiler

public class SoulWeaponStorage implements IStorage<ISoulWeapon> {
    @Override
    public INBT writeNBT(Capability<ISoulWeapon> capability, ISoulWeapon instance, Direction side) {
        CompoundNBT tag = new CompoundNBT();
        tag.putString("adventureitems.weaponType", instance.getCurrentType().getName());
        return tag;
    }
    . . .
}

public class SoulWeaponProvider implements ICapabilitySerializable<INBT> {
    @CapabilityInject(ISoulWeapon.class)
    public static Capability<ISoulWeapon> TYPE;
    private LazyOptional<ISoulWeapon> instance = LazyOptional.of(TYPE::getDefaultInstance);
    . . .
}

 

Edited by transfarmer
Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Why have you not shown this?

I apologize. The issue with writeNBT was that I was trying to use Object.toString() on a value that was null.The provider class is shown below.

Spoiler

public class SoulWeaponProvider implements ICapabilitySerializable<INBT> {
    @CapabilityInject(ISoulWeapon.class)
    public static Capability<ISoulWeapon> TYPE;
    private LazyOptional<ISoulWeapon> instance = LazyOptional.of(TYPE::getDefaultInstance);

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, Direction side) {
        return capability == TYPE ? instance.cast() : LazyOptional.empty();
    }

    @Override
    public INBT serializeNBT() {
        return TYPE.getStorage().writeNBT(TYPE, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty")), null);
    }

    @Override
    public void deserializeNBT(INBT nbt) {
        TYPE.getStorage().readNBT(TYPE, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty")), null, nbt);
    }
}

 

 

6 minutes ago, diesieben07 said:

This makes no sense.

I agree. Does it have to be initialized? If so, then what should it be?

Link to comment
Share on other sites

Just now, diesieben07 said:

So your issue is fixed?

Yes.

 

4 minutes ago, diesieben07 said:

I misread, for some reason I read this to be in your storage class. Your code is fine.

Then could you explain to me what it does? I'm still not sure about why I need it. Is it a basic instance made for every object to which the capability is attached for NBT storage? Also, I think that my data are not saved after leaving and joining a world. Do I have to do it manually?

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

    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Galeri555 adalah bocoran slot rekomendasi gacor dari Galeri555 yang bisa anda temukan di SLOT Galeri555. Situs SLOT Galeri555 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Galeri555 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Galeri555 merupakan SLOT Galeri555 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Galeri555. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Galeri555 hari ini yang telah disediakan SLOT Galeri555. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Galeri555 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Galeri555 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Galeri555 di link SLOT Galeri555.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Kocok303 adalah bocoran slot rekomendasi gacor dari Kocok303 yang bisa anda temukan di SLOT Kocok303. Situs SLOT Kocok303 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Kocok303 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Kocok303 merupakan SLOT Kocok303 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Kocok303. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Kocok303 hari ini yang telah disediakan SLOT Kocok303. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Kocok303 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Kocok303 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Kocok303 di link SLOT Kocok303.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Slot Aster88 adalah bocoran slot rekomendasi gacor dari Aster88 yang bisa anda temukan di SLOT Aster88. Situs SLOT Aster88 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Aster88 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Aster88 merupakan SLOT Aster88 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Aster88. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Aster88 hari ini yang telah disediakan SLOT Aster88. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Aster88 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Aster88 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Aster88 di link SLOT Aster88.
    • 🚀Link Daftar Klik Disini🚀 Tips Bermain Slot Bank Jago agar Meraih Maxwin dan Jackpot di MAXWINBET77 Bermain slot online Bank jago adalah cara yang seru dan mengasyikkan untuk mencari keuntungan besar di MAXWINBET77. Jika kamu ingin meningkatkan peluangmu untuk meraih maxwin dan jackpot secara terus-menerus, ada beberapa tips dan strategi yang bisa kamu terapkan. Berikut adalah panduan lengkapnya: Pilih Slot dengan RTP Tinggi: RTP (Return to Player) adalah persentase rata-rata dari total taruhan yang dikembalikan kepada pemain sebagai kemenangan. Pilihlah mesin slot Bank jago yang memiliki RTP tinggi, karena ini meningkatkan peluangmu untuk meraih kemenangan dalam jangka panjang. Kenali Fitur Bonus: Setiap slot Bank jago memiliki fitur bonus yang berbeda, seperti putaran gratis, simbol liar (wild), dan bonus game. Pelajari dengan baik fitur-fitur ini karena mereka dapat membantu meningkatkan peluang meraih kemenangan besar. Kelola Taruhan dengan Bijak: Tentukan batasan taruhan yang sesuai dengan budget dan jangan tergoda untuk bertaruh melebihi kemampuan finansialmu. Terapkan strategi taruhan yang bijak untuk memaksimalkan penggunaan saldo. Mainkan Slot Bank jago Progresif: Jika tujuanmu adalah meraih jackpot besar, coba mainkan slot Bank jago progresif di MAXWINBET77. Jackpot pada jenis slot Bank ini terus bertambah seiring dengan taruhan pemain lainnya, sehingga dapat mencapai jumlah yang sangat besar. Manfaatkan Promosi dan Bonus: MAXWINBET77 sering kali menawarkan promosi dan bonus kepada pemainnya. Manfaatkan bonus-bonus ini untuk meningkatkan peluangmu meraih kemenangan tanpa menggunakan modal tambahan. Berkonsentrasi dan Bersabar: Fokuslah saat bermain slot bank jago dan jangan terburu-buru. Bersabarlah meskipun tidak langsung mendapatkan hasil yang diharapkan. Kadang-kadang diperlukan waktu dan keberuntungan untuk mencapai maxwin atau jackpot. Baca Aturan Permainan: Sebelum bermain, pastikan untuk membaca aturan dan pembayaran pada slot Bank Jago yang dipilih. Mengetahui cara kerja mesin slot akan membantu mengoptimalkan strategi bermainmu. Dengan menerapkan tips-tips di atas dan tetap bermain secara bertanggung jawab, kamu dapat meningkatkan peluang meraih maxwin dan jackpot di Slot Bank Jago MAXWINBET77. Selamat bermain dan semoga sukses meraih kemenangan besar Anda Hari Ini.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 SLOT BCA 10K adalah bocoran slot rekomendasi gacor dari RATUASIA77 yang bisa anda temukan di SLOT BCA 10K. Situs SLOT BCA 5K hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT BSI 5K terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT BCA 10K merupakan SLOT BCA 10K hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT BSI 10K. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT BCA 10K hari ini yang telah disediakan SLOT BCA 10K. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs RATUASIA77 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT BCA 10K terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT BCA 10K di link SLOT BCA RATUASIA77.
  • Topics

×
×
  • Create New...

Important Information

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