Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [SOLVED]NullPointerException reading a value from DataWatcher
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
The_Fireplace

[SOLVED]NullPointerException reading a value from DataWatcher

By The_Fireplace, August 9, 2016 in Modder Support

  • Start new topic

Recommended Posts

The_Fireplace    11

The_Fireplace

The_Fireplace    11

  • Creeper Killer
  • The_Fireplace
  • Forge Modder
  • 11
  • 241 posts
Posted August 9, 2016

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

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15995 posts
Posted August 9, 2016

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.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • cadbane86140
      Minecraft: Cereal UHC Season 2 Episode 5- THE FINALE!

      By cadbane86140 · Posted 2 hours ago

      Hello There! A bit of a late upload due to technical issues but ITS UP! So after a week of Cereal UHC we have finally reached the end of it! I am so thankful to be apart of this series and I can't wait for the next season! There are so many hilarious moments especially during the final battle! I hope you all enjoyed this series and if you did don't forget to like and sub for more!  
    • samjviana
      [1.16.5] How to make EnchantedBook go to Custom ItemGroup

      By samjviana · Posted 2 hours ago

      But how can i do that for an enchantment book, for normal items i was able to do that by calling the function "group" as you said, but with the enchantment seems to be different.
    • poopoodice
      [1.16.5] How to make EnchantedBook go to Custom ItemGroup

      By poopoodice · Posted 3 hours ago

      Just create an instance of ItemGroup, and set the group of the item in the item property builder.
    • poopoodice
      Trying to make a crop have an "X" shape model

      By poopoodice · Posted 3 hours ago

      Check the parent of their block model.
    • LexManos
      Forge says this file does not have an app associated with it.

      By LexManos · Posted 3 hours ago

      Some zip managers like to take control of the .jar file extension away from Java. Make sure you have Java installed and try running Jarfix once, then try the installer again.
  • Topics

    • cadbane86140
      0
      Minecraft: Cereal UHC Season 2 Episode 5- THE FINALE!

      By cadbane86140
      Started 2 hours ago

    • samjviana
      2
      [1.16.5] How to make EnchantedBook go to Custom ItemGroup

      By samjviana
      Started 6 hours ago

    • TheMajorN
      1
      Trying to make a crop have an "X" shape model

      By TheMajorN
      Started 5 hours ago

    • ESCCarp
      3
      Forge says this file does not have an app associated with it.

      By ESCCarp
      Started 7 hours ago

    • hammy3502
      0
      [1.16.4] Fluid Flowing Very Oddly

      By hammy3502
      Started 4 hours ago

  • Who's Online (See full list)

    • brok4d
    • hammy3502
    • Swordstriker113
    • samvoidx
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [SOLVED]NullPointerException reading a value from DataWatcher
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community