Jump to content

[1.8] [Solved] NullPointerException network problem


Recommended Posts

I've made a test package to learn IMessage and IMessage handler and it works fine if i play singleplayer and multiplayer but when I try to connect with a second player LAN or server the second player will get a NullPointerException when i try to find the player


Here's the package and handler

public class TestPacket implements IMessage{

private int test;

public TestPacket() {}

public TestPacket(int test) {
	this.test = test;

public void toBytes(ByteBuf buf) {

public void fromBytes(ByteBuf buf) {
	test = buf.readInt();

public static class Handler implements IMessageHandler<TestPacket,IMessage>{

	public IMessage onMessage(TestPacket message, MessageContext ctx) {
				EntityPlayer player = (EntityPlayer) Minecraft.getMinecraft().theWorld.getEntityByID(message.test);
				player.addChatComponentMessage(new ChatComponentText("Awesome!"));
		return null;


how i make the network channel

public void preInit(FMLPreInitializationEvent event)
	FMLCommonHandler.instance().bus().register(new ConfigurationHandler());
	MinecraftForge.EVENT_BUS.register(new RegisterEvent());

	Network = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.NETWORKCHANNEL);
	Network.registerMessage(TestPacket.Handler.class,TestPacket.class, 0,Side.CLIENT);



The package gets send on PlayerLoggedInEvent

public void onEntityJoinWorld(PlayerEvent.PlayerLoggedInEvent event) {


What it calls in my IEEP

	public void SyncWithServer(){
	VampireMain.Network.sendTo(new TestPacket(player.getEntityId()),(EntityPlayerMP) player);


Here's the Log from the second client



Link to comment
Share on other sites

As of 1.8 packets are handled asynchronously. That means: Not on the main minecraft thread.

If you want to interact with the world or players or anything like that from a packet handler you need to pass a Runnable that does the actual work to IThreadListener#addScheduledTask. The IThreadListener implementation is Minecraft.getMinecraft() on the client and the world on the server.

thanks it works now :)


but would that not mean that the first player also should return NullPointerException?


also where can I find some doc on the IThreadListener? can't hurt to read up on it :)

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.

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.

  • Create New...

Important Information

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