Solved NullPointerException on List<String>

Discussion in 'Plugin Development' started by webbhead, Aug 8, 2015.

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

    webbhead

    Hey, Im getting a NullPointerException when I initiate my List<String> from config.

    The line that has the error is where it says: (THIS IS THE LINE)

    Code:
        @SuppressWarnings({ "deprecation" })
        public static void openShop(Player p) {
           
            Inventory inv = Bukkit.createInventory(null, 54, "§c§lToken Shop");
    
            (THIS IS THE LINE) List<String> slots = Main.getInstance().getConfig().getStringList("active");
            for (String slot : slots) {
                ItemStack item = new ItemStack(Material.getMaterial(Main.getInstance().getConfig().getInt("inventory.slots." + slot + ".type")), Main.getInstance().getConfig().getInt("inventory.slots." + slot + ".amount"));
                ItemMeta m = item.getItemMeta();
                List<String> lores = Main.getInstance().getConfig().getStringList("inventory.slots." + slot + ".lore");
                List<String> nLores = new ArrayList<String>();
                for (String lore : lores) {
                    nLores.add(ChatColor.translateAlternateColorCodes('&', lore));
                }
                m.setDisplayName(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("inventory.slots." + slot + "name")));
                m.setLore(nLores);
                inv.setItem(Main.getInstance().getConfig().getInt("inventory.slots." + slot + ".slot-number"), item);
                }
            p.openInventory(inv);
        }
    }
     
  2. Offline

    adam753

    Code:
    Main.getInstance().getConfig().getStringList("active");
    In the above, either Main.getInstance() is returning null, or getStringList is returning null because there is no such list in the config. I can't remember what it does in that situation.
     
  3. Offline

    teej107

    Main.getInstance() returns null

    Assuming he is following naming conventions which he does look like he is, Main is the class and getInstance is a static method. Even if getStringList returns a null value, that won't cause an NPE. Only when you try to access something from the null value later on.
     
  4. Offline

    webbhead

    In my main class I am calling ShopInv.openShop(player); which is calling the List<String> but I do not know how to fix this! :eek:
     
  5. Offline

    adam753

    Spotted that a millisecond after posting, I thought Main was a field variable for some reason.

    Good spot.

    @webbhead
    Find out why Main.getInstance() is returning null. You're probably not setting the variable it returns.
     
  6. Offline

    webbhead

    Main.getInstance() works fine for everything else, idk why that would be the problem. Anyways in my main class I have this. (My main class is called Main):

    Code:
    Plugin plugin;
    
    public static Plugin getInstance() { return plugin; }
     
  7. Offline

    teej107

    Do you know why NullPointerExceptions happen?
     
  8. Offline

    webbhead

    Never really looked into why Exception happen, but I am sure it is, because what the path is pointing to is null. I know what most exceptions are (that I have used)... I am noob :/

    EDIT: Just looked it up I understand what an NPE is now but I still don't understand how to fix it.
     
  9. Offline

    teej107

    It's an easy fix. Make sure the objects you are using aren't null and make sure you initialize your variables.
     
  10. Offline

    webbhead

    But, I did initialize my variable, by
    slots = Main.getInstance().getConfig().getStringList("active");
     
  11. Offline

    teej107

    @webbhead No. That is not what I'm taking about. The value returned from Main.getInstance() is null. Make sure the value being returned by Main.getInstance() is initialized and not null.

    Basically your code statement right now is this:
    Code:
    //     v---- your getInstance method return value
    Main.null.getConfig().............
     
  12. @webbhead You should try adding this at the begining of your class:

    Code:
        JavaPlugin plugin;
    
        public YourClass(Main plugin) {
            this.plugin = plugin;
        }
    Then just get the config like so:


    Code:
    plugin.getConfig().getStringList("active");
    Hope it helps! :):)
     
  13. Class class; < This would be null

    class = new Class(); < class is no longer not null :)

    Calling class when it is null will throw a NPE but when it isn't null it will work fine
     
Thread Status:
Not open for further replies.

Share This Page