-6

I have a really strange issue with variable initialization during a constructor call, in certain classes.

This issue occurs in that project and for example on that line.

I tried to debug it and even at the end of initEntity() method the variable pickupDelay has its value, but if I try to print its value after that method, it's magically null. I have similar problems if I try to extend class Entity, and initialize its fields, during constructor call. And variable is not set to null anywhere in the code.

This probably doesn't happen to static variables.

So maybe better question would be, what can cause that already initialized (final or not final) variablse are set to null.

short video about the issue https://youtu.be/otizERrYSVU

my code to test the issue:

CompoundTag itemTag = NBTIO.putItemHelper(Item.get(0));
        itemTag.setName("Item");

        CompoundTag nbt = new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", 0))
                .add(new DoubleTag("", 0)).add(new DoubleTag("", 0)))
                .putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", 0))
                        .add(new DoubleTag("", 0)).add(new DoubleTag("", 0)))
                .putList(new ListTag<FloatTag>("Rotation")
                        .add(new FloatTag("", new java.util.Random().nextFloat() * 360))
                        .add(new FloatTag("", 0)))
                .putShort("Health", 5).putCompound("Item", itemTag).putShort("PickupDelay", 40); //value 40

        EntityItem item = new EntityItem(this.defaultLevel.getChunk(0, 0, true), nbt); //new instance

item.pickupDelay changes to 0 unexpectedly

Honza Bednář
  • 396
  • 4
  • 14
  • 2
    Did you build your code on an indian burial ground? Variables don't magically get changed to null. Either something explicitly does it, or you're looking at different variables. – Kayaman Sep 02 '17 at 10:59
  • No, they probably does, or JVM does that, for example: method init() is called in the constructor on the end of init() method, the variable has value 10, I also print its value in the constructor immediately after the init() method call, and output in console is 10 and 0. I can provide an example code to reproduce this. – Honza Bednář Sep 02 '17 at 12:04
  • 1
    [then please provide a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – P.J.Meisch Sep 02 '17 at 13:29
  • Unfortunately I have not "working" short example, because it happens only in this case. I uploaded short video about it. – Honza Bednář Sep 02 '17 at 14:33
  • Help us reproduce your problem, and you will get reply. – user3437460 Sep 02 '17 at 14:34
  • The only way I know about to reproduce it is to test it with Nukkit software, how I did in the video – Honza Bednář Sep 02 '17 at 14:42

1 Answers1

0

I solved it,

The variable is declared as

public int pickupDelay = 0;

and value is changed during parent constrctor call, so its probably overriden after that.

Honza Bednář
  • 396
  • 4
  • 14