Jump to content

[SOLVED] [1.15.2] NullPointerException: Saving entity NBT


LK1905

Recommended Posts

Hello, I've been stuck on a problem that I don't know how to fix.

 

Crash report:

Description: Saving entity NBT

java.lang.NullPointerException: Saving entity NBT
	at com.lk1905.gielinorcraft.capability.CommonCapabilityProvider.serializeNBT(CommonCapabilityProvider.java:45) ~[?:?] {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:1523) ~[?:?] {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:114) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:659) [?:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66] {}


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

-- Head --
Thread: Server thread
Stacktrace:
	at com.lk1905.gielinorcraft.capability.CommonCapabilityProvider.serializeNBT(CommonCapabilityProvider.java:45)
	at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:127)
	at net.minecraftforge.common.capabilities.CapabilityProvider.serializeCaps(CapabilityProvider.java:86)

-- Entity being saved --
Details:
	Entity Type: minecraft:player (net.minecraft.entity.player.ServerPlayerEntity)
	Entity ID: 435
	Entity Name: Dev
	Entity's Exact location: -229.50, 64.00, -165.50
	Entity's Block location: World: (-230,64,-166), Chunk: (at 10,4,10 in -15,-11; contains blocks -240,0,-176 to -225,255,-161), Region: (-1,-1; contains chunks -32,-32 to -1,-1, blocks -512,0,-512 to -1,255,-1)
	Entity's Momentum: 0.00, -0.08, 0.00
	Entity's Passengers: []
	Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
	at net.minecraft.entity.Entity.writeWithoutTypeId(Entity.java:1523)
	at net.minecraft.server.integrated.IntegratedPlayerList.writePlayerData(IntegratedPlayerList.java:27)
	at net.minecraft.server.management.PlayerList.saveAllPlayerData(PlayerList.java:631)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:114)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:659)
	at java.lang.Thread.run(Thread.java:745)

-- System Details --
Details:
	Minecraft Version: 1.15.2
	Minecraft Version ID: 1.15.2
	Operating System: Windows 10 (amd64) version 10.0
	Java Version: 1.8.0_66, Oracle Corporation
	Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
	Memory: 249239288 bytes (237 MB) / 651689984 bytes (621 MB) up to 820510720 bytes (782 MB)
	CPUs: 4
	JVM Flags: 1 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump
	ModLauncher: 5.0.0-milestone.4+67+b1a340b
	ModLauncher launch target: fmluserdevclient
	ModLauncher naming: mcp
	ModLauncher services: 
		/eventbus-2.0.0-milestone.1-service.jar eventbus PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar object_holder_definalize PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar runtime_enum_extender PLUGINSERVICE 
		/accesstransformers-2.0.0-milestone.1-shadowed.jar accesstransformer PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar capability_inject_definalize PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar runtimedistcleaner PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar fml TRANSFORMATIONSERVICE 
	FML: 31.1
	Forge: net.minecraftforge:31.1.0
	FML Language Providers: 
		[email protected]
		minecraft@1
	Mod List: 
		client-extra.jar Minecraft {[email protected] DONE}
		forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-recomp.jar Forge {[email protected] DONE}
		main Gielinorcraft {[email protected] DONE}
	Player Count: 1 / 8; [ServerPlayerEntity['Dev'/435, l='New World', x=-229.50, y=64.00, z=-165.50]]
	Data Packs: vanilla, mod:forge (incompatible), mod:gielinorcraft (incompatible)
	Type: Integrated Server (map_client.txt)
	Is Modded: Definitely; Client brand changed to 'forge'

 

CommonCapabilityProvider.java

package com.lk1905.gielinorcraft.capability;

import javax.annotation.Nullable;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

public class CommonCapabilityProvider<HANDLER> implements ICapabilitySerializable<INBT>{

	protected final Capability<HANDLER> capability;
	protected final Direction side;
	protected final HANDLER instance;
	
	protected final LazyOptional<HANDLER> lazyOptional;
	
	public CommonCapabilityProvider(final Capability<HANDLER> capability, @Nullable final Direction side, @Nullable final HANDLER instance) {
		
		this.capability = capability;
		this.side = side;
		this.instance = instance;
		
		if(this.instance != null) {
			
			lazyOptional = LazyOptional.of(() -> this.instance);
			
		}else {
			
			lazyOptional = LazyOptional.empty();
		}
	}

	@Override
	public <T> LazyOptional<T> getCapability(final Capability<T> cap, @Nullable final Direction side) {
		
		return capability.orEmpty(cap, lazyOptional);
	}

	@Nullable
	@Override
	public INBT serializeNBT() {

		return capability.writeNBT(getInstance(), getFacing());//<-- THIS LINE
	}

	@Override
	public void deserializeNBT(INBT nbt) {

		capability.readNBT(getInstance(), getFacing(), nbt);
	}
	
	@Nullable
	public Direction getFacing() {
		return side;
	}
	
	@Nullable
	public HANDLER getInstance() {
		return instance;
	}
}

 

Other code if you need to see it is here:

https://github.com/LK1905/GielinorCraft

 

 

Link to comment
Share on other sites

23 minutes ago, DragonITA said:

Why you need NBT Data from entity?

 

I'm working on a capability that should allow me to create rpg-like skills, and I need to save the data from those skills to the player.

 

This is my CapabilitySkills class, in case the problem is actually in there:

package com.lk1905.gielinorcraft.capability.skill;

import com.lk1905.gielinorcraft.api.capability.ISkillContainer;
import com.lk1905.gielinorcraft.api.utils.CapabilityUtils;
import com.lk1905.gielinorcraft.capability.CommonCapabilityProvider;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class CapabilitySkills {

	@CapabilityInject(ISkillContainer.class)
	private static Capability<ISkillContainer> skillContainerCapability;
	
	public static void register() {
		
		CapabilityManager.INSTANCE.register(ISkillContainer.class, new SkillStorage(), () -> new SkillContainer());//<---THIS LINE
	}
	
	@SubscribeEvent
	public static void attachCapability(AttachCapabilitiesEvent<Entity> e) {
		
		if(!(e.getObject() instanceof PlayerEntity)) {
			return;
		}
		
		SkillContainer newContainer = new SkillContainer();
		
		e.addCapability(newContainer.getCapabilityID(), 
						new CommonCapabilityProvider<>(skillContainerCapability, null, newContainer));
	}
	
	@SubscribeEvent
	public static void onPlayerClone(final PlayerEvent.Clone e) {
		
		ISkillContainer oldCapability = (ISkillContainer) CapabilityUtils.getCapability(e.getOriginal(), skillContainerCapability);
		ISkillContainer newCapability = (ISkillContainer) CapabilityUtils.getCapability(e.getPlayer(), skillContainerCapability);
		
		newCapability.setAllSkills(oldCapability.getAllSkills());
	}
	
	public static ISkillContainer getSkillContainer(ICapabilityProvider provider) {
		
		return (ISkillContainer) CapabilityUtils.getCapability(provider, skillContainerCapability);
	}
	
	public static Capability<ISkillContainer> getSkillCapability(){
		
		return skillContainerCapability;
	}
}

 

Link to comment
Share on other sites

I solved the problem. It had nothing to do with the Capability or the Provider class, but with how I was registering it in my Main class. I had the Capability registered under FMLCommonEvent, when I should have registered it under a FMLServerStartingEvent.

 

So anyway, Problem Solved!

Link to comment
Share on other sites

  • LK1905 changed the title to [SOLVED] [1.15.2] NullPointerException: Saving entity NBT
18 minutes ago, diesieben07 said:

This is not true.

FMLServerStartingEvent can fire multiple times in a game session (every time you load a save).

Capabilities should be registered in FMLCommonSetupEvent.

 

I tried moving it back, and now i get this crash:

Description: Loading entity NBT

java.lang.NullPointerException: Loading entity NBT
	at com.lk1905.gielinorcraft.capability.CommonCapabilityProvider.deserializeNBT(CommonCapabilityProvider.java:51) ~[?:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityDispatcher.deserializeNBT(CapabilityDispatcher.java:139) ~[?:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.deserializeCaps(CapabilityProvider.java:96) ~[?:?] {re:classloading}
	at net.minecraft.entity.Entity.read(Entity.java:1600) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.management.PlayerList.readPlayerDataFromFile(PlayerList.java:272) ~[?:?] {re:classloading}
	at net.minecraft.server.management.PlayerList.initializeConnectionToPlayer(PlayerList.java:112) ~[?:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tryAcceptPlayer(ServerLoginNetHandler.java:119) ~[?:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:63) ~[?:?] {re:classloading}
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:241) ~[?:?] {re:classloading}
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:148) ~[?:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:900) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:818) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:659) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:745) ~[?:1.8.0_66] {}


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

-- Head --
Thread: Render thread
Stacktrace:
	at com.lk1905.gielinorcraft.capability.CommonCapabilityProvider.deserializeNBT(CommonCapabilityProvider.java:51)
	at net.minecraftforge.common.capabilities.CapabilityDispatcher.deserializeNBT(CapabilityDispatcher.java:139)
	at net.minecraftforge.common.capabilities.CapabilityProvider.deserializeCaps(CapabilityProvider.java:96)

-- Entity being loaded --
Details:
	Entity Type: minecraft:player (net.minecraft.entity.player.ServerPlayerEntity)
	Entity ID: 260
	Entity Name: Dev
	Entity's Exact location: -99.06, 64.00, 174.46
	Entity's Block location: World: (-100,64,174), Chunk: (at 12,4,14 in -7,10; contains blocks -112,0,160 to -97,255,175), Region: (-1,0; contains chunks -32,0 to -1,31, blocks -512,0,0 to -1,255,511)
	Entity's Momentum: 0.00, -0.08, 0.00
	Entity's Passengers: []
	Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
	at net.minecraft.entity.Entity.read(Entity.java:1600)
	at net.minecraft.server.management.PlayerList.readPlayerDataFromFile(PlayerList.java:272)
	at net.minecraft.server.management.PlayerList.initializeConnectionToPlayer(PlayerList.java:112)
	at net.minecraft.network.login.ServerLoginNetHandler.tryAcceptPlayer(ServerLoginNetHandler.java:119)
	at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:63)
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:241)

-- Ticking connection --
Details:
	Connection: net.minecraft.network.NetworkManager@7b4641db
Stacktrace:
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:148)
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:900)
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:818)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:659)
	at java.lang.Thread.run(Thread.java:745)

-- System Details --
Details:
	Minecraft Version: 1.15.2
	Minecraft Version ID: 1.15.2
	Operating System: Windows 10 (amd64) version 10.0
	Java Version: 1.8.0_66, Oracle Corporation
	Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
	Memory: 259039432 bytes (247 MB) / 643301376 bytes (613 MB) up to 820510720 bytes (782 MB)
	CPUs: 4
	JVM Flags: 1 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump
	ModLauncher: 5.0.0-milestone.4+67+b1a340b
	ModLauncher launch target: fmluserdevclient
	ModLauncher naming: mcp
	ModLauncher services: 
		/eventbus-2.0.0-milestone.1-service.jar eventbus PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar object_holder_definalize PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar runtime_enum_extender PLUGINSERVICE 
		/accesstransformers-2.0.0-milestone.1-shadowed.jar accesstransformer PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar capability_inject_definalize PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar runtimedistcleaner PLUGINSERVICE 
		/forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-launcher.jar fml TRANSFORMATIONSERVICE 
	FML: 31.1
	Forge: net.minecraftforge:31.1.0
	FML Language Providers: 
		[email protected]
		minecraft@1
	Mod List: 
		client-extra.jar Minecraft {[email protected] DONE}
		forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-recomp.jar Forge {[email protected] DONE}
		main Gielinorcraft {[email protected] DONE}
	Player Count: 0 / 8; []
	Data Packs: vanilla, mod:forge (incompatible), mod:gielinorcraft (incompatible)
	Type: Integrated Server (map_client.txt)
	Is Modded: Definitely; Client brand changed to 'forge'

 

Instead of "saving Entity NBT", I now have a problem with "Loading Entity NBT".

 

When I had the capability registered in FMLServerStartingEvent, I had no problem.

Link to comment
Share on other sites

11 minutes ago, diesieben07 said:

And did you try loading two saves after each other? Your registration code should crash if its in FMLServerStartingEvent.

 

You're right, when I try to open a second world I get this crash:

Description: Exception in server tick loop

java.lang.IllegalArgumentException: Cannot register a capability implementation multiple times : com.lk1905.gielinorcraft.api.capability.ISkillContainer
	at net.minecraftforge.common.capabilities.CapabilityManager.register(CapabilityManager.java:72) ~[forge-1.15.2-31.1.0_mapped_snapshot_20190719-1.14.3-recomp.jar:?] {re:classloading}
	at com.lk1905.gielinorcraft.capability.skill.CapabilitySkills.register(CapabilitySkills.java:26) ~[main/:?] {re:classloading}
	at com.lk1905.gielinorcraft.Gielinorcraft.serverStarting(Gielinorcraft.java:42) ~[main/:?] {re:classloading}
	at net.minecraftforge.eventbus.ASMEventHandler_2_Gielinorcraft_serverStarting_FMLServerStartingEvent.invoke(.dynamic) ~[?:?] {}
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:80) ~[eventbus-2.0.0-milestone.1-service.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258) ~[eventbus-2.0.0-milestone.1-service.jar:?] {}
	at net.minecraftforge.fml.server.ServerLifecycleHooks.handleServerStarting(ServerLifecycleHooks.java:90) ~[?:?] {re:classloading}
	at net.minecraft.server.integrated.IntegratedServer.init(IntegratedServer.java:101) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:635) [?:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66] {}

 

Link to comment
Share on other sites

  • LK1905 changed the title to [1.15.2] NullPointerException: Saving entity NBT

No, I'm not sure.

 

This is my Main class:

package com.lk1905.gielinorcraft;

import org.apache.logging.log4j.Logger;

import com.lk1905.gielinorcraft.api.skills.Skill;
import com.lk1905.gielinorcraft.capability.skill.CapabilitySkills;
import com.lk1905.gielinorcraft.capability.skill.SkillContainer;
import com.lk1905.gielinorcraft.skills.AttackSkill;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;

@Mod(Gielinorcraft.MODID)
@Mod.EventBusSubscriber
public class Gielinorcraft {

	public static Logger logger;
	
	public static final String MODID = "gielinorcraft";
	
	public Gielinorcraft() {
		
	}
	
	@SubscribeEvent
	public static void onCommonSetup(FMLCommonSetupEvent e) {

		CapabilitySkills.register();
		
		SkillContainer.registerNewSkill(AttackSkill.class);
		
		MinecraftForge.EVENT_BUS.register(Skill.class);
		MinecraftForge.EVENT_BUS.register(AttackSkill.class);
	}
}

 

Is that how its supposed to be added? Or is there something else I need to do?

Link to comment
Share on other sites

19 hours ago, diesieben07 said:

Then check?! Learn how to use the debugger.

 

FMLCommonSetupEvent fires on the mod event bus, you are only registering your class to the forge bus. Check the bus parameter of EventBusSubscriber.

 

I added this after @Mod.EventBusSubscriber

@Mod.EventBusSubscriber(modid = Gielinorcraft.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)

 

And now everything works, thank you!

Link to comment
Share on other sites

  • LK1905 changed the title to [SOLVED] [1.15.2] NullPointerException: Saving entity NBT

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.