Solved NullPointerException in my plugin.

Discussion in 'Plugin Development' started by wyld, Mar 28, 2020.

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

    wyld

    Hello,

    I'm trying to code a plugin where player data is accessed through a players.yml file. Before I run any data-pulling methods, I check if the configuration section corresponding to the player exists, so the code that pulls player data does not run if there is no section for the player.

    I am coming across a NullPointerException on lines that should only run when the player data section exists. My YML file is blank.

    Please see my error below
    Code:
    Caused by: java.lang.NullPointerException
    at com.wyld.thimbleduels.ConfigManager.getPlayers(ConfigManager.java:27) ~[?:?]
    at com.wyld.thimbleduels.GUIs.DuelsMenu.setItemMetas(DuelsMenu.java:68) ~[?:?]
    at com.wyld.thimbleduels.GUIs.DuelsMenu.<init>(DuelsMenu.java:39) ~[?:?]
    at com.wyld.thimbleduels.Commands.Thimble.onCommand(Thimble.java:19) ~[?:?]
    
    Next, here is the method from which the error is coming:

    Code:
        public void setItemMetas() {
            if(invite) {
                targetheadMeta = (SkullMeta) targethead.getItemMeta();
            }
            headMeta = (SkullMeta) head.getItemMeta();
            thimbleToggleMeta = thimbleToggle.getItemMeta();
            exitMeta = exit.getItemMeta();
            moneyMeta = money.getItemMeta();
            fillerMeta = filler.getItemMeta();
            headMeta.setOwningPlayer((OfflinePlayer) player);
            headMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&b&l" + player.getName()));
            if(cfgm.getPlayers().isConfigurationSection("Players." + player.getName())){
                played = cfgm.getPlayers().getInt("Players." + player.getName() + ".Played");
                wins = cfgm.getPlayers().getInt("Players." + player.getName() + ".Wins");
                losses = cfgm.getPlayers().getInt("Players." + player.getName() + ".Losses");
            }else {
                played = 0;
                wins = 0;
                losses = 0;
            }
            List<String> headLore = new ArrayList<String>();
            headLore.add(ChatColor.BLUE + "STATISTICS");
            headLore.add(ChatColor.BLUE + "Duels Played: " + ChatColor.DARK_GRAY + played);
            headLore.add(ChatColor.BLUE + "Duels Won: " + ChatColor.DARK_GRAY + wins);
            headLore.add(ChatColor.BLUE + "Duels Lost: " + ChatColor.DARK_GRAY + losses);
            // int winPercent = (wins/played)*100;
            //headLore.add(ChatColor.BLUE + "Win Percentage: " + ChatColor.DARK_GRAY + winPercent + "%");
            headMeta.setLore(headLore);
            head.setItemMeta(headMeta);
        
                
        }
    
    Thanks in advance
    wyld
     
  2. Offline

    timtower Administrator Administrator Moderator

    @wyld And what is line 27?
     
  3. Offline

    wyld

    It's line 68 that matters, it is

    Code:
    played = cfgm.getPlayers().getInt("Players." + player.getName() + ".Played");
    
     
  4. Offline

    timtower Administrator Administrator Moderator

    @wyld The first line in the trace is line 27, why do you think that the bad one is 68?
    Post both classes please, in full.
     
  5. Offline

    wyld

    It is because as a troubleshooting step, I got rid of the whole section surrounding line 68, and it worked fine (without that code of course)


    This is ConfigManager.class, line 27 is marked in **********
    Code:
    package com.wyld.thimbleduels;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import com.wyld.thimbleduels.Configs.MapsCfg;
    import com.wyld.thimbleduels.Configs.PlayersCfg;
    
    public class ConfigManager {
        private ThimbleDuels plugin = ThimbleDuels.getPlugin(ThimbleDuels.class);
        private PlayersCfg players;
        private MapsCfg maps;
        public void loadConfigurationFiles() {
            if(!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdir();
                plugin.cmd.sendMessage(ChatColor.BLUE+ "Created the plugin data folder.");
            }
            players = new PlayersCfg();
            players.loadPlayers();
            maps = new MapsCfg();
          
        }
        public void saveConfigurationFiles() {
            players.savePlayers();
            //maps.saveMaps();
        }
        public FileConfiguration getPlayers() {
            return players.getPlayers(); **************
        }
        //public FileConfiguration getMaps() {
            //return maps.getMaps();
        //}
    }
    
    
    This is class DuelsMenu.class
    Line 68 is marked in **********

    Code:
    package com.wyld.thimbleduels.GUIs;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;
    
    import com.wyld.thimbleduels.ConfigManager;
    import com.wyld.thimbleduels.VaultManager;
    import com.wyld.thimbleduels.Configs.PlayersCfg;
    
    public class DuelsMenu {
        private VaultManager vm = new VaultManager();
        private Inventory i;
        private ConfigManager cfgm = new ConfigManager();
        private Player player, target;
        private ItemStack head, targethead, thimbleToggle, exit, money, filler;
        private SkullMeta headMeta, targetheadMeta;
        private ItemMeta thimbleToggleMeta, exitMeta, moneyMeta, fillerMeta;
        private boolean invite;
        private int played, wins, losses;
        public DuelsMenu(Player player, Player target, boolean invite) {
            this.invite = invite;
            if(this.invite) {
            this.target = target;
            }
            this.player = player;
            i = Bukkit.createInventory(null, 18, ChatColor.BLUE + "ThimbleDuels");
            initializeItems();
            setItemMetas();
            addItems();
            player.openInventory(i);
        }
      
        public void initializeItems() {
            if(invite) {
                targethead = new ItemStack(Material.PLAYER_HEAD, 1);
            }
            head = new ItemStack(Material.PLAYER_HEAD, 1);
            thimbleToggle = new ItemStack(Material.RED_STAINED_GLASS_PANE, 1);
            exit = new ItemStack(Material.BARRIER, 1);
            money = new ItemStack(Material.GOLD_NUGGET, 1);
            filler = new ItemStack(Material.BLUE_STAINED_GLASS_PANE, 1);
        }
    
      
        public void setItemMetas() {
            if(invite) {
                targetheadMeta = (SkullMeta) targethead.getItemMeta();
            }
            headMeta = (SkullMeta) head.getItemMeta();
            thimbleToggleMeta = thimbleToggle.getItemMeta();
            exitMeta = exit.getItemMeta();
            moneyMeta = money.getItemMeta();
            fillerMeta = filler.getItemMeta();
            headMeta.setOwningPlayer((OfflinePlayer) player);
            headMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&b&l" + player.getName()));
            if(cfgm.getPlayers().isConfigurationSection("Players." + player.getName())){
                played = cfgm.getPlayers().getInt("Players." + player.getName() + ".Played"); ******************
                wins = cfgm.getPlayers().getInt("Players." + player.getName() + ".Wins");
                losses = cfgm.getPlayers().getInt("Players." + player.getName() + ".Losses");
            }else {
                played = 0;
                wins = 0;
                losses = 0;
            }
            List<String> headLore = new ArrayList<String>();
            headLore.add(ChatColor.BLUE + "STATISTICS");
            headLore.add(ChatColor.BLUE + "Duels Played: " + ChatColor.DARK_GRAY + played);
            headLore.add(ChatColor.BLUE + "Duels Won: " + ChatColor.DARK_GRAY + wins);
            headLore.add(ChatColor.BLUE + "Duels Lost: " + ChatColor.DARK_GRAY + losses);
            // int winPercent = (wins/played)*100;
            //headLore.add(ChatColor.BLUE + "Win Percentage: " + ChatColor.DARK_GRAY + winPercent + "%");
            headMeta.setLore(headLore);
            head.setItemMeta(headMeta);
          
                  
        }
      
      
        public void addItems() {
            i.setItem(4, head);
        }
    }
    
    
     
  6. Offline

    timtower Administrator Administrator Moderator

    @wyld You never call ConfigManager.loadConfigurationFiles(), so players is null, so players.getPlayers() will throw a nullpointer.
     
  7. Offline

    wyld

    @wyld @wyld @wyld @wyld
    Code:
    package com.wyld.thimbleduels;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.wyld.thimbleduels.Commands.Thimble;
    
    public class ThimbleDuels extends JavaPlugin{
        public ConsoleCommandSender cmd = this.getServer().getConsoleSender();
        ConfigManager cfgm;
        VaultManager vm;
        private boolean error = false;
        @Override
        public void onEnable() {
            loadConfigurationFiles();
            loadVault();
            register();
            if(!error) {
            cmd.sendMessage(ChatColor.GREEN + "ThimbleDuels has been successfully enabled.");
            }else {
                cmd.sendMessage(ChatColor.DARK_RED + "The plugin is disabling due to an error.");
                this.setEnabled(false);
            }
           
        }
       
        @Override
        public void onDisable() {
           
        }
       
        public void register() {
            getCommand("thimble").setExecutor(new Thimble());
        }
        public void loadConfigurationFiles() {
            cfgm = new ConfigManager();
            cfgm.loadConfigurationFiles();
            cfgm.saveConfigurationFiles();
        }
        public void loadVault() {
            vm = new VaultManager();
            if(vm.isVaultUsable()) {
                error = false;
            }else {
                cmd.sendMessage(ChatColor.DARK_RED + "It seems Vault is not installed, so ThimbleDuels cannot proceed with start-up.");
                error = true;
            }
           
        }
    }
    
     
  8. Offline

    timtower Administrator Administrator Moderator

    @wyld That is a different instance though, it is not the same one.
     
  9. Offline

    wyld

    I figured it out, thanks!
     
Thread Status:
Not open for further replies.

Share This Page