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


    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?

    [18:20:51 ERROR]: Could not pass event InventoryClickEvent to Particles v1.0
    org.bukkit.event.EventException: null
            at$1.execute( ~[craftbukkit.jar:git-Bukkit-3819f8d]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[craftbukkit.jar:git-Bukkit-3819f8d]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( [craftbukkit.jar:git-Bukkit-3819f8d]
            at org.bukkit.plugin.SimplePluginManager.callEvent( [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a( [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$ [craftbukkit.jar:git-Bukkit-3819f8d]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_144]
            at 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( [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D( [craftbukkit.jar:git-Bukkit-3819f8d]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C( [craftbukkit.jar:git-Bukkit-3819f8d]
            at [craftbukkit.jar:git-Bukkit-3819f8d]
            at Source) [?:1.8.0_144]
    Caused by: java.lang.NullPointerException
            at me.izerkahh.particles.Main.onClick( ~[?:?]
            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$1.execute( ~[craftbukkit.jar:git-Bukkit-3819f8d]
            ... 15 more
    Main class and only class:
    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;
    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")) {
            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");
            inv.setItem(2, hopper);
            inv.setItem(6, redstone);
        public void onMove(PlayerMoveEvent e) {
            if (smoke.contains(e.getPlayer().getUniqueId())) {
                e.getPlayer().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 5);
        public void secondMenu(Player player) {
            ItemStack item1 = new ItemStack(Material.STAINED_GLASS_PANE);
            ItemMeta item1Meta = item1.getItemMeta();
            ArrayList<String> item1Lore = new ArrayList<String>();
            part.setItem(0, item1);
        public void onClick(InventoryClickEvent e) {
                Player player = (Player) e.getWhoClicked();
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Remove particles")) {
                } else if (e.getCurrentItem().equals(Material.COAL_BLOCK)) {
  2. Offline


    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:
    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


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


    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()

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


    @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


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


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

Share This Page