Inventory is null?

Discussion in 'Plugin Development' started by blok601, Feb 14, 2016.

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

    blok601

    I am currently making a report plugin. When you report someone, their head shows up in an Inventory that staff members can view by doing /reports. For some reason, when my stack trace is saying that my inventory is null:

    Code:
    java.lang.NullPointerException
        at me.blok601.command.Reports.<init>(Reports.java:24) ~[?:?]
        at me.blok601.command.Main.onEnable(Main.java:19) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:342) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:314) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:406) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:370) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:325) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:235) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:503) [craftbukkit-1.8.8.jar:git-Bukkit-9a17f01]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Code:

    Reports Class:
    Code:
    package me.blok601.command;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    
    public class Reports implements CommandExecutor{
    
      
        Main plugin;
        Report rep;
      
        public Reports(Report rep, Main plugin){
            this.plugin = plugin;
            this.rep = rep;
        }
        Inventory reported = rep.or;
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(sender instanceof Player){
                Player p = (Player) sender;
                p.openInventory(reported);
            }else{
                sender.sendMessage(ChatColor.RED + "Only players can run this command!");
                return false;
            }
            return false;
          
        }
      
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Open Reports")){
                return;
            }else{
                Player p = (Player) e.getWhoClicked();
                e.setCancelled(true);
                if(e.getCurrentItem() == null || e.getCurrentItem().getType()  == Material.AIR || !e.getCurrentItem().hasItemMeta()){
                    p.closeInventory();
                    return;
                }else{
                    Material b = e.getCurrentItem().getType();
                    reported.remove(b);
                    p.sendMessage(ChatColor.GREEN + "Report marked as solved!");
                }
            }
        }
    
    }
    

    Report Class:
    Code:
    package me.blok601.command;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.SkullMeta;
    public class Report implements CommandExecutor{
      
        public Inventory or = Bukkit.createInventory(null, 54, ChatColor.GOLD + "" + ChatColor.BOLD + "Open Reports");
      
        private void add(Player player, String reason){
            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
            SkullMeta meta = (SkullMeta) skull.getItemMeta();
          
            meta.setOwner(player.getName());
            meta.setDisplayName(ChatColor.LIGHT_PURPLE + player.getName());
    
          
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GREEN + reason);
            meta.setLore(lore);
          
            skull.setItemMeta(meta);
          
            or.addItem(skull);
        }
    
        Main plugin;
      
        public Report(Main plugin){
            this.plugin = plugin;
        }
      
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(sender instanceof Player){
                Player p = (Player) sender;
                if(args.length == 0){
                    p.sendMessage("Use the command like this! /report <player> <reason>");
                    return false;
                }else if(args.length == 1){
                    p.sendMessage(ChatColor.RED + "Please supply a reason!");
                }else if(args.length >= 2){
                    StringBuilder reason = new StringBuilder();
                    for(int i = 1; i < args.length; i++){
                        reason.append(args[i]).append(" ");
                    }
                    Player target = Bukkit.getPlayer(args[0]);
                    if(target == null){
                        p.sendMessage(ChatColor.RED + "Player couldn't be found!");
                        return false;
                    }
                    p.sendMessage(ChatColor.GREEN + "Thank you for reporting " + target.getName() + "!");
                    add(target, reason.toString());
                  
                }
            }else{
                sender.sendMessage(ChatColor.RED + "Only players can report!");
                return false;
            }
            return false;
          
        }
      
      
    }
    
    Main Class:
    Code:
    package me.blok601.command;
    
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
    import net.minecraft.server.v1_8_R3.PlayerConnection;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
      
        @Override
        public void onEnable(){
            this.getCommand("report").setExecutor(new Report(this));
            this.getCommand("reports").setExecutor(new Reports(new Report(this), this));
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(sender instanceof Player){
                Player p = (Player) sender;
                PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
              
              
                if(cmd.getName().equalsIgnoreCase("forums")){
                    PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("[\"\",{\"text\":\"[Click Here to view our Forums!]\",\"color\":\"dark_purple\",\"bold\":true,\"clickEvent\":{\"action\":\"open_url\",\"value\":\"http://nightshadepvp.com/\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"CLICK TO VIEW FORUMS!\",\"color\":\"gold\"}]}}}]"));
                  
                    connection.sendPacket(packet);
                    return true;
                }else if(cmd.getName().equalsIgnoreCase("sug")){              
                    PacketPlayOutChat cp = new PacketPlayOutChat(ChatSerializer.a("[\"\",{\"text\":\"[Click here to make a suggestion!]\",\"color\":\"blue\",\"bold\":true,\"clickEvent\":{\"action\":\"open_url\",\"value\":\"http://nightshadepvp.com/comment.php?fid=4\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"THANKS FOR TAKING THE TIME TO IMPROVE THE SERVER!\",\"color\":\"dark_aqua\"}]}}}]"));
                    connection.sendPacket(cp);
                  
                  
                    return true;
                }
            }else{
                sender.sendMessage(ChatColor.RED+ "Only players can run this command!");
                return false;
            }
            return false;
          
        }
    
    }
    
    Help would certainly be appreciated, thanks!
     
  2. Offline

    teej107

    @blok601 oh geez! To keep things simple, just get in the habit of initializing fields in the constructor.
     
  3. Offline

    blok601

    What should I do about the null inventory ?

    @teej107
     
  4. Offline

    teej107

    @blok601 That is what you should do about the null inventory.

    It doesn't matter where the fields are located in your class. They will always get declared and ran before the constructor gets called.
     
  5. Offline

    blok601

    That never even went through my brain...thank you. I will fix that tommorrow morning.

    EDIT: I fixed it all, thank you very much! @teej107

    But, I have ran into another issue. Whenever I add an item to the inventory, it doesn't get added. If I need to show my code, just tell me
     
    Last edited: Feb 15, 2016
  6. Offline

    ycraft1

    Make a item meta for the item and then:

    Inventory.addItem (1, STONE);

    STONE = the item meta
     
  7. Offline

    blok601

    What do you mean?
     
  8. Offline

    teej107

    You sure you are adding it? Run debug code to make sure it is actually being added.
     
  9. Offline

    blok601

    Yes, I have a debug message that i am receiving.
     
  10. Offline

    StarRocket

    Try updating the inventory after adding the item.
     
  11. Offline

    teej107

    @blok601 You don't have multiple instances of the same inventory right? Try what @StarRocket suggested
     
  12. Offline

    blok601

    @teej107 is there an update inventory method?
     
  13. Offline

    teej107

    @blok601 Yes but I believe it's for the player's own inventory and not others.
     
  14. Offline

    StarRocket

    @blok601 should just be player.updateInventory(); and replace player with whatever is viewing the inventory
    this works for other inventories because it fixed an issue I had earlier with the slot 0 of workbench inventories being invisible
    When you run the code that opens the inventory, just update the inventory after that
     
Thread Status:
Not open for further replies.

Share This Page