Solved Static initialization error

Discussion in 'Spigot Plugin Development' started by Schottky, Jan 10, 2020.

  1. So this is probably an error where I am just too blind to see what the cause is. Anyhow, here comes. I have this class:
    Code (Java):
    public abstract class CosmeticParticleTypes {

        public static final CosmeticParticleType<ParticleFireRing> FIRE_RING;

        static {
            FIRE_RING = register(new CosmeticParticleType<>(ParticleFireRing::new, "fire_ring"));
        }

        private static List<CosmeticParticleType<? extends CosmeticParticle>> allValues = new ArrayList<>();

        private static <T extends CosmeticParticle> CosmeticParticleType<T> register(CosmeticParticleType<T> particle) {
            assert allValues != null;
            allValues.add(particle);    // Null-pointer exception here
            return particle;
        }
    }

    This is the CosmeticParticleTypeClass:

    Code (Java):
    public class CosmeticParticleType<T extends CosmeticParticle> extends CosmeticType<T> {

        public CosmeticParticleType(IFactory<T> particleFactory, String uniqueName) {
            this.particleFactory = particleFactory;
            this.uniqueName = uniqueName;
        }

        public T instantiate(World world) {
            return particleFactory.create(world);
        }

       public interface IFactory<T extends CosmeticParticle> {
            T create(World world);
        }
    }

    I call the particle on a test command like this:

    Code (Java):
    final ParticleFireRing particleFireRing = CosmeticParticleTypes.FIRE_RING.instantiate(player.getWorld());   //
    ExceptionInInitializerError
    particleFireRing.setPlayer(player);
    particleFireRing.tick();
    It works all well if I do not register the particle but just instantiate it.

    However, if I don't do this, this is the error:

    Code (Text):

    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin flareCosmetics v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.base/java.lang.Thread.run(Thread.java:830) [?:?]
    Caused by: java.lang.ExceptionInInitializerError
        at me.schottky.cosmetics.command.CommandTest.onCommand(CommandTest.java:31) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        ... 15 more
    Caused by: java.lang.NullPointerException
        at me.schottky.cosmetics.particle.CosmeticParticleTypes.register(CosmeticParticleTypes.java:24) ~[?:?]
        at me.schottky.cosmetics.particle.CosmeticParticleTypes.<clinit>(CosmeticParticleTypes.java:16) ~[?:?]
        at me.schottky.cosmetics.command.CommandTest.onCommand(CommandTest.java:31) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
     

    I suppose this is because I need to first statically initialize the constant, but I am not sure about this.
    Any help is greatly appreciated!
     
    #1 Schottky, Jan 10, 2020
    Last edited: Jan 10, 2020
  2. Caused by: java.lang.NullPointerException
    at me.schottky.cosmetics.particle.CosmeticParticleTypes.register(CosmeticParticleTypes.java:24) ~[?:?]
     
  3. I know, but none of the fields are null.
    The two options that could be null are the particle (not null) or the list (not null)
    The initializer should have no problem with null-values
     
  4. what line is 24? the assert? get rid of the assert, you really dont need it if youre declaring the field anyway
     
  5. agreed, was to check if it was null

    Edit: the places where the exception occurs are marked
     
  6. I found the error myself. The List containing all values (allValues) has to be initialized before the other values are initialized.
    This makes sense from a logical point of view, but it's completely bonkers when you try and print out
    Code (Text):
    System.out.println(allValues == null)
    and it prints out false, even thou it's true.
    here's the code snippet that needs to get changed

    Code (Java):
    public static final CosmeticParticleType<ParticleFireRing> FIRE_RING;
    private static List<CosmeticParticleType<? extends CosmeticParticle>> allValues;

        static {
            allValues = new ArrayList<>();
            FIRE_RING = register(new CosmeticParticleType<>(ParticleFireRing::new, "fire_ring"));
        }
     
  7. I thought that was the problem but I couldn't find a reference to support it. Its too early for static blocks, I need more caffeine....