Solved NullPointerException on adding to new Initialized ArrayList

Discussion in 'Plugin Development' started by ipodtouch0218, Aug 4, 2016.

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

    ipodtouch0218

    Hey guys.
    I'm doing a plugin request in the Plugin Request forums, and while I was developing I noticed errors while testing.

    Looking at my stacktrace, it is a NullPointerException on 86, where I .add(<stringhere>) to a String ArrayList, which doesn't really make sense to me because I initialized it.

    If I enter a wrong material name for args[1], it strangely now passes 86 and NPE's on 87, where I set it to the config.

    (I know it's inefficient on all the try's and catch'es, it's just temporary.)


    Code:
    Code:
                        if (args[0].equalsIgnoreCase("add")) {
                            Material addItem = Material.AIR;
                            Material addBlock = Material.AIR;
                            try {
                                addItem = Material.matchMaterial(args[1]);
                            } catch (Exception e) {
                                sender.sendMessage(ChatColor.RED + "\"" + ChatColor.YELLOW + args[1] + ChatColor.RED + "\" is not a Material!");
                                return true;
                            }
                            try {
                                addBlock = Material.matchMaterial(args[2]);
                            } catch (Exception e) {
                                sender.sendMessage(ChatColor.RED + "\"" + ChatColor.YELLOW + args[2] + ChatColor.RED + "\" is not a Material!");
                                return true;
                            }
                           
                            List<String> itemList = new ArrayList<String>(); //ArrayList initiated here
                            try {
                                itemList = getConfig().getStringList(addItem.toString()); //If it exists from config, get it.
                            } catch (NullPointerException e) {
                           
                            }
                            itemList.add(addBlock.toString()); //Line 86 (NullPointer if args[1] + args[2] are correct
                            getConfig().set(addItem.toString(), itemList); //Line 87 (NullPointer if args[1] doesnt match a Enum Material
                            reloadConfig();
                            return true;
                        }
     
  2. Offline

    ArsenArsen

    matchMaterial returns material or null, does not throw the no enum constant exception. Thats your issue.
     
    ipodtouch0218 likes this.
  3. @ipodtouch0218
    I doubt the ArrayList is null. My guess is the "addBlock" variable. Remember that matchMaterial doesn't throw exceptions when it doesn't find a material, it returns null.

    EDIT: Ninja'd
     
    ipodtouch0218 likes this.
  4. Offline

    ipodtouch0218

Thread Status:
Not open for further replies.

Share This Page