Help with a NullPointerException

Discussion in 'Spigot Plugin Development' started by ChikenGamer, Jun 21, 2015.

  1. Error:
    HTML:
    [19:40:57 ERROR]: Error occurred while enabling AdvancedHubPlus v1.0.0 (Is it up
    to date?)
    java.lang.NullPointerException
            at com.jimdo.choscar.Principal.GADGETS.Hats.<init>(Hats.java:24) ~[?:?]
            at com.jimdo.choscar.Principal.AdvancedHubPlusMain.onEnable(AdvancedHubP
    lusMain.java:26) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[s
    pigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:340) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.jav
    a:356) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.
    java:316) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:4
    18) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:3
    82) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:3
    37) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.jav
    a:256) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :528) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Line 24 Hats:
    PHP:
     String id1 = plugin.getConfig().getString("Hat1.Hat");
    Line 26 Main:
    PHP:
     pm.registerEvents(new Hats(this), this);
    Class of hats:
    Code (Text):
    public class Hats implements Listener{
       
      AdvancedHubPlusMain plugin;
        public Hats(AdvancedHubPlusMain plugin) {
            this.plugin = plugin;
        }
    ////////////////////Hat 1///////////////////////
            String id1 = plugin.getConfig().getString("Hat1.Hat");
            int ip1 = plugin.getConfig().getInt("Hat1.Short");
            int Hat1 = plugin.getConfig().getInt("Hat1.Slot");
            String Hat1Name = plugin.getConfig().getString("Hat1.Name").replaceAll("&", "§");
            String Hat1Lore = plugin.getConfig().getString("Hat1.Lore").replaceAll("&", "§");
    [MORE CODE OF THE SAME]
    public Inventory invH = Bukkit.createInventory(null, 18, "Name");  
       //private Inventory invH;
        public void MenuHats() { //to onEnable
             
                    invH.setItem(Hat1, createItem(Material.valueOf(id1), 1, (short) ip1, Hat1Name, Hat1Lore));
                    invH.setItem(Hat2, createItem(Material.valueOf(id2), 1, (short) ip2, Hat2Name, Hat2Lore));
                    invH.setItem(Hat3, createItem(Material.valueOf(id3), 1, (short) ip3, Hat3Name, Hat3Lore));
                    invH.setItem(Hat4, createItem(Material.valueOf(id4), 1, (short) ip4, Hat4Name, Hat4Lore));
                    invH.setItem(Hat5, createItem(Material.valueOf(id5), 1, (short) ip5, Hat5Name, Hat5Lore));
                    invH.setItem(Hat6, createItem(Material.valueOf(id6), 1, (short) ip6, Hat6Name, Hat6Lore));
                    invH.setItem(Hat7, createItem(Material.valueOf(id7), 1, (short) ip7, Hat7Name, Hat7Lore));
                    invH.setItem(Hat8, createItem(Material.valueOf(id8), 1, (short) ip8, Hat8Name, Hat8Lore));
                   
                    ////////////////////////////////////////////////////////////////////////////////////////
                   
                    invH.setItem(Hat9, createItem(Material.valueOf(id9), 1, (short) ip9, Hat9Name, Hat9Lore));
                    invH.setItem(Hat10, createItem(Material.valueOf(id10), 1, (short) ip10, Hat10Name, Hat10Lore));
                    invH.setItem(Hat11, createItem(Material.valueOf(id11), 1, (short) ip11, Hat11Name, Hat11Lore));
                    invH.setItem(Hat12, createItem(Material.valueOf(id12), 1, (short) ip12, Hat12Name, Hat12Lore));
                    invH.setItem(Hat13, createItem(Material.valueOf(id13), 1, (short) ip13, Hat13Name, Hat13Lore));
                    invH.setItem(Hat14, createItem(Material.valueOf(id14), 1, (short) ip14, Hat14Name, Hat14Lore));
                    invH.setItem(Hat15, createItem(Material.valueOf(id15), 1, (short) ip15, Hat15Name, Hat15Lore));
                    invH.setItem(Hat16, createItem(Material.valueOf(id16), 1, (short) ip16, Hat16Name, Hat16Lore));
                    invH.setItem(Hat17, createItem(Material.valueOf(id17), 1, (short) ip17, Hat17Name, Hat17Lore));
                    invH.setItem(Hat18, createItem(Material.valueOf(id18), 1, (short) ip18, Hat18Name, Hat18Lore));
             
        }
               
         
            private ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {
                    ItemStack i = new ItemStack(material, amount, (short) shrt);
                    ItemMeta im = i.getItemMeta();
                    im.setDisplayName(displayname);
                   
                    ArrayList<String> loreList = new ArrayList<String>();
                    String[] lores = lore.split("/");
                    loreList.addAll(Arrays.asList(lores));
                   
                    im.setLore(loreList);
                    i.setItemMeta(im);
                    return i;
                }
         
            public void show(Player p) {
                    p.openInventory(invH);
            }
         
            @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
                    if (!e.getInventory().getName().equalsIgnoreCase(invH.getName())) return;
                    if (e.getCurrentItem() == null) return;
                    if (e.getCurrentItem().getItemMeta() == null) return;
    [MORE CODE OF THE SAME]
                   
    Main Class:
    PHP:
    public class AdvancedHubPlusMain extends JavaPlugin implements Listener{
       
        public static Logger l = Logger.getLogger("Minecraft");
        PluginManager pm = Bukkit.getServer().getPluginManager();
        //public static Plugin plugin = null;
       
        private Hats Hats;
       
        public void onEnable(){
      /////////////////////////////////////////////////////////////////////////////  
            saveDefaultConfig();
           
            pm.registerEvents(this, this);
            pm.registerEvents(new Hats(this), this);
            l.log(Level.INFO, " > Status: Enabled");
      /////////////////////////////////////////////////////////////////////////////    
      /////////////////////////////////////////////////////////////////////////////
        Hats.MenuHats();
           
    }
        public void onDisable(){
        l.log(Level.INFO, " > Status: Disabled");
        saveConfig();
      }
    }
     
     
  2. Try printing out what plugin.getConfig().getString("Hat1.Hat") returns. That is what is likely to be null.
     
  3. Try changing your parameter name in the Hats constructor to something other than plugin. Then do: this.plugin = parametername (ex: instance).

    I think the NPE is due to "hiding" an existing variable. The first line in the exception states the error occurred while initializing the constructor/class.
     
  4. And how i try that?
     
  5. Change the parameter doesn't work
     
  6. Store result in a variable then System.out.println(learnjavavariablenamehere);
     
    • Agree Agree x 1
  7. This didn't work?
     
  8. Yes, dont work