Jump to content

[SOLVED]NullPointerException reading a value from DataWatcher


The_Fireplace

Recommended Posts

Ok, so, my initial issue was that my custom TNT was rendering as a white cube. I searched every thread I could find related to that issue, and the only real answer I found was this:

 

 

I suspect that your fuse counter (variable within your entity class) does only change within the serverside scope, which means the fuse won't be visible to the client and thus rendering your entity white all the time.

You need to sync the fuse with the client and the easiest way to do this is to use a DataWatcher.

Define a new DataWatcherObject (with this.dataWatcher.addObject(...)) within entityInit

on the server side (if worldObj.isRemote == false) set the object's value to the fuse variable in your onUpdate method after it gets updated with dataWatcher.updateObject(...)

on the client side (if worldObj.isRemote == true)  set the value of the fuse variable to the dataWatcherObject's value with getWatchableObjectInt(...)

 

If you don't understand what I mean, here have some pseudocode:

Entity Class
----------
entityInit():
    dataWatcher->addObject(id, Integer.valueOf(0))    | id must be unique! If you get a crash because an already given ID, try another one
                                                      | second param must be casted to the datatype you wanna put in there, here Integer (BTW, the cast is not pseudocode)

onUpdate(): (at the end within the method)
    if world is not remote:
        dataWatcher->updateObject(id, fuse)    | id must be the same as the above defined one
    else:
        fuse = dataWatcher->getWatchableObjectInt(id)    | id, same as above

http://www.minecraftforge.net/forum/index.php/topic,16627.msg84436.html#msg84436

 

 

So, I tried following what it said, and it seems that when I try to read the data from the DataWatcher, it throws a NullPointerException repeatedly as long as the entity exists:

 

 

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: java.lang.NullPointerException

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.entity.DataWatcher.getWatchableObjectInt(DataWatcher.java:93)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at the_fireplace.wars.entities.EntityPTNTPrimed.onUpdate(EntityPTNTPrimed.java:73)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2011)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.world.World.updateEntity(World.java:1976)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.world.World.updateEntities(World.java:1805)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.client.Minecraft.runTick(Minecraft.java:2176)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1080)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.client.Minecraft.run(Minecraft.java:380)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.client.main.Main.main(Main.java:116)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at java.lang.reflect.Method.invoke(Method.java:497)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at java.lang.reflect.Method.invoke(Method.java:497)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at GradleStart.main(GradleStart.java:26)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at java.lang.reflect.Method.invoke(Method.java:497)

[20:37:10] [Client thread/INFO]: [the_fireplace.wars.entities.EntityPTNTPrimed:onUpdate:75]: at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

 

 

And here is the important part of my code:

 

 

@Override
protected void entityInit() {
	if(!worldObj.isRemote)
		this.dataWatcher.addObject(fuseID, Integer.valueOf(fuse));
}

@Override
public void onUpdate() {
	if(!worldObj.isRemote)
		dataWatcher.updateObject(fuseID, Integer.valueOf(fuse));
	else
		try {
			fuse = dataWatcher.getWatchableObjectInt(fuseID);
		}catch(Exception e){
			e.printStackTrace();
		}

 

 

The part surrounded by try/catch is what throws the NPE. If you need to see the full file, here is a link to it on github.

Could anyone tell me what I am doing wrong?

If I helped please press the Thank You button.

 

Check out my mods at http://www.curse.com/users/The_Fireplace/projects

Link to comment
Share on other sites

My guess would be this:

 

@Override
protected void entityInit() {
	if(!worldObj.isRemote) //why is this here?
		this.dataWatcher.addObject(fuseID, Integer.valueOf(fuse));
}

 

Based on looking at what Vanilla does, e.g. minecarts:

 

    protected void entityInit()
    {
        this.dataManager.register(ROLLING_AMPLITUDE, Integer.valueOf(0));
        this.dataManager.register(ROLLING_DIRECTION, Integer.valueOf(1));
        this.dataManager.register(DAMAGE, Float.valueOf(0.0F));
        this.dataManager.register(DISPLAY_TILE, Integer.valueOf(0));
        this.dataManager.register(DISPLAY_TILE_OFFSET, Integer.valueOf(6));
        this.dataManager.register(SHOW_BLOCK, Boolean.valueOf(false));
    }

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

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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