1.16.5 "Null pointer Exception"

Discussion in 'Spigot Plugin Development' started by TheAspie_, Jun 30, 2021.

  1. So I'm trying to make a simple plugin that uses a BlockBreakEvent to cancel the breaking of a block that is set in the config.yml. I keep getting a Null Pointer Exception error, and was wondering if anyone could give some insight. I'm still pretty new to plugin development, so I might be missing something obvious lol

    EDIT: My config.yml is litterally just
    "blocks:
    -STONE
    -DIRT"

    Code (Java):
    import org.bukkit.command.*;
    import org.bukkit.configuration.file.*;
    import org.bukkit.entity.*;
    import org.bukkit.event.*;
    import org.bukkit.event.block.*;
    import org.bukkit.plugin.java.*;
    import java.util.*;
    public final class UnbreakableBlocks extends JavaPlugin implements Listener, CommandExecutor {

            final FileConfiguration cfg = this.getConfig();
            @Override
            public void onEnable() {
                    this.getServer().getPluginManager().registerEvents(this, this);
                    this.saveDefaultConfig();
                    getConfig().options().copyDefaults(true);
                    saveConfig();}




            @EventHandler
            public void onBlockBreak(BlockBreakEvent event) {
                    Player player = event.getPlayer();
                    player.sendMessage(String.valueOf(this.cfg.getConfigurationSection("blocks").getKeys(true)));
                    this.getConfig().getConfigurationSection("blocks").getKeys(true).forEach(key -> {

                            if (key.equalsIgnoreCase(String.valueOf(event.getBlock().getType().toString()))) {
                                    event.setCancelled(true); }});}
            @Override
            public void onDisable() {}

    }
     

    Server thread/ERROR Could not pass event BlockBreakEvent to UnbreakableBlocks v1.0
    30.06 14:50:23 [Server] INFO org.bukkit.event.EventException: null
    30.06 14:50:23 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerInteractManager.breakBlock(PlayerInteractManager.java:309) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerInteractManager.a(PlayerInteractManager.java:268) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerInteractManager.a(PlayerInteractManager.java:238) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerConnection.a(PlayerConnection.java:1219) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.PacketPlayInBlockDig.a(SourceFile:40) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.PacketPlayInBlockDig.a(SourceFile:10) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.TickTask.run(SourceFile:18) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.ba(MinecraftServer.java:941) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.executeNext(MinecraftServer.java:934) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.sleepForTick(MinecraftServer.java:918) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:850) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 14:50:23 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
    30.06 14:50:23 [Server] INFO Caused by: java.lang.NullPointerException
    30.06 14:50:23 [Server] INFO at me.theaspie.unbreakableblocks.UnbreakableBlocks.onBlockBreak(UnbreakableBlocks.java:21) ~[?:?]
    30.06 14:50:23 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_292]
    30.06 14:50:23 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_292]
    30.06 14:50:23 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_292]
    30.06 14:50:23 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_292]
     
    #1 TheAspie_, Jun 30, 2021
    Last edited: Jun 30, 2021

  2. there are few things that could be null

    blocks section or the keys and also by checking name using enums you can use .name() instead of toString(); also it is already a string so you don't need to use String.valueOf
     
  3. My guess is the line:
    Code (Java):
    final FileConfiguration cfg = this.getConfig();
    You initialize its value the moment it's created, at which point the config is non-existent.
    That is why you use "getConfig()" each time you need it instead of defining a variable for it - to avoid that problem.
    Remove the variable and change the code accordingly and let us know if it worked.
    If not, I'll take a look a deeper look into the code to make sure nothing else is wrong.
     
  4. In which line is the error?
    Note, use:
    Code (Java):
    //getKeys in false
    getConfig().getConfigurationSection("blocks").getKeys(false)
     
  5. What's on line number 21 of your main class? That looks to be the source of your NPE.
     
  6. The line that causes the problem is line 21 :
    Code (Java):
    player.sendMessage(String.valueOf(this.cfg.getConfigurationSection("blocks").getKeys(true)));
    How to fix it:
     
    • Like Like x 1
  7. I removed that line, since it wasn't really needed regardless and changed some things that other recommended, but I still get the error. I think it's because it's not recognising the Keys, which I don't understand
    Code (Java):
    import org.bukkit.command.*;
    import org.bukkit.entity.*;
    import org.bukkit.event.*;
    import org.bukkit.event.block.*;
    import org.bukkit.plugin.java.*;
    public final class UnbreakableBlocks extends JavaPlugin implements Listener, CommandExecutor {
            @Override
            public void onEnable() {
                    this.getServer().getPluginManager().registerEvents(this, this);
                    this.saveDefaultConfig();
                    getConfig().options().copyDefaults(true);
                    saveConfig();}
            @EventHandler
            public void onBlockBreak(BlockBreakEvent event) {
                    this.getConfig().getConfigurationSection("blocks").getKeys(true).forEach(key -> {
                            if (key.equalsIgnoreCase(event.getBlock().getType().name())) {
                                    event.setCancelled(true); }});}
            @Override
            public void onDisable() {}

    }




     
     
  8. Which line here is causing the NPE?
     
  9. I have no idea... it's telling me it's line 26, but there isn't a line 26...

    Server thread/ERROR Could not pass event BlockBreakEvent to UnbreakableBlocks v1.0
    30.06 19:28:26 [Server] INFO org.bukkit.event.EventException: null
    30.06 19:28:26 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerInteractManager.breakBlock(PlayerInteractManager.java:309) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerInteractManager.a(PlayerInteractManager.java:268) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerInteractManager.a(PlayerInteractManager.java:238) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerConnection.a(PlayerConnection.java:1219) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.PacketPlayInBlockDig.a(SourceFile:40) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.PacketPlayInBlockDig.a(SourceFile:10) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.TickTask.run(SourceFile:18) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.ba(MinecraftServer.java:941) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.executeNext(MinecraftServer.java:934) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.sleepForTick(MinecraftServer.java:918) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:850) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.2.jar:git-Spigot-379750e-9a9c223]
    30.06 19:28:26 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
    30.06 19:28:26 [Server] INFO Caused by: java.lang.NullPointerException
    30.06 19:28:26 [Server] INFO at me.theaspie.unbreakableblocks.UnbreakableBlocks.onBlockBreak(UnbreakableBlocks.java:26) ~[?:?]
    30.06 19:28:26 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_292]
    30.06 19:28:26 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_292]
    30.06 19:28:26 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_292]
    30.06 19:28:26 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_292]
     
  10. Make sure it's your most recent code, so compile the jar, put it in your plugin folder, try to get the error again. Then paste both the code and the error inside a pastebin please :)
     
  11. TheAspie Code - Pastebin.com
     
  12. Please do the code in a different pastebin, there we can see what line is exactly causing the error :p
     
  13. My guess is either the block is null (maybe air is null) or one of the keys read is null.
    Change the function to this and give it a try:
    Code (Java):
    public void onBlockBreak(BlockBreakEvent event) {
        if (event.getBlock() == null || event.getBlock().getType() == null) return;
        String type = event.getBlock().getType().name();
        ConfigurationSection section = this.getConfig().getConfigurationSection("blocks");
        if (section == null) return;
        Set<String> keys = section.getKeys(true);
        if (keys == null || keys.isEmpty()) return;
        for (String str : keys)
            if (key.equalsIgnoreCase(type)) {
                event.setCancelled(true);
                break;
            }
    }
    This can be shortened even more but for now it's ok, just to get you started.
     
  14.  
  15. My bad, change this line:
    Code (Java):
    for (String str : keys)
    to this:
    Code (Java):
    for (String key : keys)
    and it should work fine.
     
  16. The thing is blocks is not a section in config, it's just a string list, so it's even simpler: if (config.getStringList("path").contains(block type))
     
  17. Oh yeah... nice
     
    • Like Like x 1
  18. Where does that go though?
     
  19. Code (Java):
    public void onBlockBreak(BlockBreakEvent event) {
        if (event.getBlock() != null && event.getBlock().getType() != null && config.getStringList("path").contains(event.getBlock().getType().name()))
            event.setCancelled(true);
    }
     
  20. config returns an error?