Solved Can't figure out how to fix this NullPointerException

Discussion in 'Plugin Development' started by iZerkahh, Sep 4, 2017.

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

    iZerkahh

    When you do a command it opens a GUI, However when I click on an empty space it will give me an error, I can't figure it out... It says to look at line 101 and when I goto that line and when I left click the item with the display name "Remove Particles" it will work fine, so why is it giving an error?

    Code:
    [18:20:51 ERROR]: Could not pass event InventoryClickEvent to Particles v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[craftbukkit.jar:git-Bukkit-3819f8d]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-3819f8d]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:498) [craftbukkit.jar:git-Bukkit-3819f8d]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:483) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1808) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-3819f8d]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_144]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_144]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:695) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:366) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:650) [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit.jar:git-Bukkit-3819f8d]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.NullPointerException
            at me.izerkahh.particles.Main.onClick(Main.java:101) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_144]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:296) ~[craftbukkit.jar:git-Bukkit-3819f8d]
            ... 15 more
    Main class and only class:
    Code:
     
    package me.izerkahh.particles;
    
    import java.util.ArrayList;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        ArrayList<UUID> smoke = new ArrayList<UUID>();
        private Inventory part;
        Inventory part1 = Bukkit.createInventory(null, 36, "Choose Particle");
    
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        public void onDisable() {
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
    
            if (label.equalsIgnoreCase("pe")) {
                createMenu(player);
            }
    
            return false;
    
        }
    
        // MAIN MENU
        public void createMenu(Player player) {
            Inventory inv = Bukkit.getServer().createInventory(null, 9, "Particle Menu");
    
            ItemStack hopper = new ItemStack(Material.HOPPER);
            ItemStack redstone = new ItemStack(Material.REDSTONE_BLOCK);
            ItemMeta hopperMeta = hopper.getItemMeta();
            ItemMeta redstoneMeta = redstone.getItemMeta();
    
            ArrayList<String> redstoneLore = new ArrayList<String>();
    
            hopperMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "List of your particles");
            redstoneMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Remove particles");
            redstoneMeta.setLore(redstoneLore);
    
            hopper.setItemMeta(hopperMeta);
            redstone.setItemMeta(redstoneMeta);
    
            inv.setItem(2, hopper);
            inv.setItem(6, redstone);
    
            player.openInventory(inv);
    
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
    
            if (smoke.contains(e.getPlayer().getUniqueId())) {
                e.getPlayer().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 5);
            }
        }
    
        // CHOOSE PARTICLES
        public void secondMenu(Player player) {
    
            ItemStack item1 = new ItemStack(Material.STAINED_GLASS_PANE);
            ItemMeta item1Meta = item1.getItemMeta();
    
            ArrayList<String> item1Lore = new ArrayList<String>();
    
            item1Lore.add("Test");
    
            item1Meta.setLore(item1Lore);
            item1.setItemMeta(item1Meta);
    
            part.setItem(0, item1);
        }
    
        @EventHandler
        public void onClick(InventoryClickEvent e) {
                Player player = (Player) e.getWhoClicked();
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Remove particles")) {
                    e.setCancelled(true);
                    player.sendMessage("REMOVED");
                    smoke.remove(e.getWhoClicked().getUniqueId());
                } else if (e.getCurrentItem().equals(Material.COAL_BLOCK)) {
                    e.setCancelled(true);
                }
        }
    }
     
  2. Offline

    MasterDoctor

    Firstly, you should instantiate your variables inside onEnable, not when the class is instantiated because these two things are not the same thing - so the top of your class should look like this:
    Code:
    public class Main extends JavaPlugin implements Listener {
    
        ArrayList<UUID> smoke;
        private Inventory part;
        Inventory part1;
    
        public void onEnable() {
           smoke =new ArrayList<UUID>();
           part1 = Bukkit.createInventory(null, 36, "Choose Particle");
    
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
    Next instead of just using @SuppressWarnings("depreciation"), you should try and find a non-depreciated method (deprecated means it's going to be removed in the future.)

    Finally, it'd be more helpful if you posted line 101... :p
     
  3. Offline

    iZerkahh

    sorry, line 101: if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Remove particles")) {
     
  4. Offline

    MasterDoctor

    Okay the reason this is occurring is that the empty space is an AIR item stack which doesn't have item meta. (This does of course apply to other item stacks that don't have item meta - I'm just explaining the empty space.)

    In order to fix it, you can simply check if the item has meta: e.getCurrentItem().hasItemMeta()

    Code:
    if (e.getCurrentItem().hasItemMeta() && e.getCurrentItem().getItemMeta().getDisplayName().contains("Remove particles")) {
     
  5. Offline

    Zombie_Striker

    @MasterDoctor @iZerkahh
    Not entirely.

    Air does not have itemmeta because Air itemstacks do not exist. When you change the type to AIR, the itemstack instance get set to null. If you want to make sure the item is not air, null check the item first.
     
    iZerkahh and MasterDoctor like this.
  6. Offline

    MasterDoctor

    Oh! Sorry, my bad :)
     
    iZerkahh likes this.
  7. Offline

    iZerkahh

    Thank you , I fixed it :D
     
Thread Status:
Not open for further replies.

Share This Page