Null getConfigurationSelection

Discussion in 'Plugin Development' started by KAM202, Jul 4, 2019.

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

    KAM202

    Hi. It's me again xD
    I can't take warp names from the "warps.yml" file.
    example warps.yml
    Code:
    warps:
      first:
        world:
        ......
      second:
        world:
        ......
    i'm using
    Code:
    warps = confWarp.getConfigurationSection("warps").getKeys(false);
    and i've got NullPointerException... why?

    main:
    Code:
    public Warps warps;
    ...
    public void onEnable() {
            this.warps = new Warps(this);
                    this.warps.setup();
    ....
    and warps.setup() :
    Code:
    public void setup() {
            file = new File(plugin.getDataFolder(), "warps.yml");
            if(!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdir();
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            confWarp = YamlConfiguration.loadConfiguration(file);
            warps = confWarp.getConfigurationSection("warps").getKeys(false);
           
        }
    ofc above: public Set<String> warps;


    Why it's not working?
     
  2. Offline

    KarimAKL

    @KAM202 Which line throws the NullPointerException?
     
  3. Offline

    KAM202

    @KarimAKL

    Code:
    warps = confWarp.getConfigurationSection("warps").getKeys(false);
    It works only if there is at least one warp saved

    when I added the check "if(confWarp.file.lenght != 0)" ok but when i want add warp so i must check if warps Set is not empty like: if(!this.plugin.warps.warps.isEmpty()) and this now returning NULL

    so how to write to read a blank file when set that Set is emplty not null?
     
  4. Offline

    KarimAKL

    @KAM202 Get the configuration section and then check if it's null.
    Code:Java
    1. ConfigurationSection section = confWarp.getConfigurationSection("warps");
    2. if (section == null) return;
    3. warps = section.getKeys(false);
     
  5. Offline

    KAM202

    @KarimAKL

    okay but when i want /setwarp <name> i've got null here:
    Code:
    if(!this.plugin.warps.warps.isEmpty()) {
     
  6. Offline

    KarimAKL

    @KAM202 I'm guessing you've set the second 'warps' to null instead of an empty list? Could you show where you set the variables 'plugin', 'warps', and 'warps'?
     
  7. Offline

    KAM202

    @KarimAKL

    Warps.java
    Code:
    package io.github.kam202;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class Warps {
        public Main plugin;
        
    
        FileConfiguration confWarp;
        File file;
        public Set<String> warps;
       
        public Warps(Main plugin) {
            this.plugin = plugin;
        }
    
        public void setup() {
            file = new File(plugin.getDataFolder(), "warps.yml");
            if(!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdir();
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            confWarp = YamlConfiguration.loadConfiguration(file);
           
            ConfigurationSection section = confWarp.getConfigurationSection("warps");
            if (section == null) return;
            warps = section.getKeys(false);
        }
    
        public FileConfiguration getConfig() {
            return confWarp;
        }
    
        public String getName() {
            return file.getName();
        }
    
        public void setWarp(String warp, World world, double x, double y, double z, float yaw, float pitch) {
            confWarp.set("warps." + warp + ".world", world.getName());
            confWarp.set("warps." + warp + ".x", x);
            confWarp.set("warps." + warp + ".y", y);
            confWarp.set("warps." + warp + ".z", z);
            confWarp.set("warps." + warp + ".yaw", yaw);
            confWarp.set("warps." + warp + ".pitch", pitch);
    
            warps.add(warp);
            //conf.set("warplist", warps);
            saveConfig();
        }
    
        public Location getWarp(String warp) {
            World w = Bukkit.getWorld(confWarp.getString("warps." + warp + ".world"));
            double x = confWarp.getDouble("warps." + warp + ".x");
            double y = confWarp.getDouble("warps." + warp + ".y");
            double z = confWarp.getDouble("warps." + warp + ".z");
            float yaw = (float) confWarp.getDouble("warps." + warp + ".yaw");
            float pitch = (float) confWarp.getDouble("warps." + warp + ".pitch");
    
            return new Location(w, x, y, z, yaw, pitch);
        }
    
        public void removeWarp(String warp) {
            confWarp.set("warps." + warp + ".world", null);
            confWarp.set("warps." + warp + ".x", null);
            confWarp.set("warps." + warp + ".y", null);
            confWarp.set("warps." + warp + ".z", null);
            confWarp.set("warps." + warp + ".yaw", null);
            confWarp.set("warps." + warp + ".pitch", null);
    
            warps.remove(warp);
            //conf.set("warplist", warp);
            saveConfig();
        }
    
        public boolean warpExists(String warp) {
            if (confWarp.getConfigurationSection("warps." + warp) == null) {
                return false;
            } else {
                return true;
            }
        }
    
        public void saveConfig() {
            try {
                confWarp.save(file);
            } catch (IOException e) {
                Bukkit.getConsoleSender().sendMessage("Could not save conf file!");
            }
        }
    
        public void reloadConfig() {
            confWarp = YamlConfiguration.loadConfiguration(file);
        }
    }
    Main.java
    Code:
        public Warps warps;
        //public PlayerData playerData;
        SetwarpCommand setwarpCommand;
        WarpCommand warpCommand;
        protected static Main plugin;
       
       
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
            this.warps = new Warps(this);
            //this.playerData = new PlayerData(this);
            this.setwarpCommand = new SetwarpCommand(this);
            this.warpCommand = new WarpCommand(this);
            this.warps.setup();
        }
     
  8. Offline

    KarimAKL

    @KAM202 I don't see 'if(!this.plugin.warps.warps.isEmpty()) {' anywhere in that code.
     
  9. Offline

    KAM202

    @KarimAKL

    ahh sorry
    Warp command class:
    Code:
    Main plugin;
        public WarpCommand(Main plugin) {
            this.plugin = plugin;
            this.plugin.getCommand("warp").setExecutor(this);
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (sender instanceof Player) {
                final Player player = (Player)sender;
                if(args.length == 0) {
                    if(!this.plugin.warps.warps.isEmpty()) {
                    //if(this.plugin.warps.file.length() != 0) {
                        StringBuilder sb = new StringBuilder();
                        for (String s : this.plugin.warps.warps) {
                            sb.append(s).append(" ");       
                        }
                        String message = sb.toString();
                        player.sendMessage(ChatColor.GOLD + "Available warps: " + ChatColor.YELLOW + message);
                        player.sendMessage(ChatColor.GOLD + "Use" + ChatColor.YELLOW + "/warp <nazwa> " + ChatColor.GOLD + " to teleport!");
                    }else {
                        sender.sendMessage(ChatColor.RED + "There are no warps!");
                    }
                   
                     
                }
               
            }
     
  10. Offline

    KarimAKL

    @KAM202 I'm guessing the Set<String> 'warps' in your 'Warps' class is null. 'section == null' inside your 'setup' method is returning true which then stops the method from assigning a value to 'warps'.
    Show your 'warps.yml' file when you do this.
     
Thread Status:
Not open for further replies.

Share This Page