Solved NullPointerException when calling Block.setType

Discussion in 'Plugin Development' started by jrlambs, Feb 4, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    jrlambs

    Hi everyone, I'm an experienced Java developer, but brand new to Minecraft/Bukkit plugin development. I've seen few youtube videos where people do things like change the block type every time someone walks on a block.. so while I'm learning how to do things I thought that would be simple/fun thing to implement in a plugin.

    I've gotten a few basic things to work (increase player speed every time they fill a bucket).. things like that.. but every time I try to interact with a block I end up with a NullPointerException, I've looked through examples and can't see anything different they're doing so I almost wonder if it's a setting I need to change on the server.

    This is the function I have right now.

    @
    Code:
    @EventHandler
        public void onPlayerMoveEvent(PlayerMoveEvent event) {
            Block blockUnderFeet = event.getFrom().getBlock().getRelative(BlockFace.DOWN,1);
            getLogger().info(blockUnderFeet.getType().toString());
            if (!blockUnderFeet.isEmpty()) {
                blockUnderFeet.setType(Material.COBBLESTONE, true);
            }
        }
    Every time this code runs it results in the following exception
    Code:
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
    Caused By:
    Code:
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData.newData(CraftBlockData.java:544) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.createBlockData(CraftServer.java:1858) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.createBlockData(CraftServer.java:1833) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.Bukkit.createBlockData(Bukkit.java:1346) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.Material.createBlockData(Material.java:3363) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at org.bukkit.craftbukkit.v1_15_R1.block.CraftBlock.setType(CraftBlock.java:164) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
        at com.jrlambs.spigotmods.firstmod.FirstMod.onPlayerMoveEvent(FirstMod.java:72) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_242]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_242]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.15.2.jar:git-Spigot-800b93f-e911c70]
    I've tried null checks leading into the code and the log statement I have in the code always prints an actual block type, I haven't ever seen it print "AIR", which I hear can be tricky one. The NPE appears to be from somewhere inside the bukkit libraries so I'm curious if anyone might know off the top of their head what could be wrong.

    Compiled using Java SDK 1.8. Running on Sigot Server git-Spigot-800b93f-e911c70 (MC: 1.15.2) (Implementing API version 1.15.2-R0.1-SNAPSHOT)

    Any help advice anyone can offer would be much appreciated. Thanks in advance.
     
  2. Offline

    KarimAKL

    @jrlambs What is line 72 of FirstMod.java?

    I can't imagine a NullPointerException getting thrown from the code you provided.

    EDIT: After a closer look, it seems line 72 is blockUnderFeet.setType(...), is that correct? Assuming that's correct, i still don't see what could be causing it, i'll try looking at some source code, maybe i can find a clue.
     
    Last edited by a moderator: Feb 4, 2020
  3. Offline

    Kars

    It is indeed strange.
    @jrlambs have you tried using setType with only the material as parameter? Without the true as a second one.
    You should avoid using extra parameters unless necessary. This is all i can think of.
    Also with Block.getRelative; the second parameter is unnecessary if you are using 1.
     
  4. Offline

    Strahan

    I copied that and it worked fine for me, everywhere I went cobble formed under my feet.
     
  5. Offline

    jrlambs

    Hmmm I've definitely tried it without the second parameter, I only added that because nothing else was working. Is it possible some setting on the server could be causing it to not work? world started out as a single player world on 1.15.1, then I played on it using a vanilla server for a bit, then I moved it to a Spigot server on 1.15.2, maybe there's some property that it expects that the blocks in my world don't have?

    I just noticed the message before the NPE:
    [17:44:10] [Server thread/ERROR]: Could not pass event PlayerMoveEvent to FirstMod v0.0.1
    org.bukkit.event.EventException: null

    Could it be something with the way I'm registering the event listener?
    Code:
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.player.*;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class FirstMod extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            getLogger().info("First Mod sadfasdfasdfasdfasdf has been enabled");
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onPlayerMoveEvent(PlayerMoveEvent event) {
            Block blockUnderFeet = event.getFrom().getBlock().getRelative(BlockFace.DOWN,1);
            getLogger().info(blockUnderFeet.getType().toString());
            if (!blockUnderFeet.isEmpty()) {
                blockUnderFeet.setType(Material.COBBLESTONE, true);
            }
        }
    
        @Override
        public void onDisable() {
            getLogger().info("First Mod has been disabled sdfasdfasdfsdf.");
        }
    }
    UPDATE: brand new world, same issue.

    On a whim I downloaded the Bukkit jar file and started it using "java -jar craftbukkit-1.15.2.jar", it started up saying it was outdated (even though it was a brand new download from the website) but when I connected to it the plugin worked perfectly, cobblestone everywhere I walked. This must be an issue specifically with the Spigot server version I was running. I ran "java -jar BuildTools.jar --rev latest" and let it build me a new Spigot jar file and now it works fine on the spigot server also.

    Just bad luck for my first shot at this apparently to also run into that bug. Thanks for your help everyone!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 5, 2020
  6. Online

    timtower Administrator Administrator Moderator

    @jrlambs Check the error, check which variable is null.
     
Thread Status:
Not open for further replies.

Share This Page