Solved For loop

Discussion in 'Spigot Plugin Development' started by TheSupreme, Apr 18, 2017.

  1. I'm unsure what is wrong here, could someone point it out to me?
    Code (Text):
            String id = "";
           
            for(Entry<String, ClanObject> entry : ClanManager.Clans.entrySet()) {
                ClanObject clanObject = entry.getValue();
                if (clanObject.getName().equals(arg))
                    id = clanObject.Id;
            }
    Error
    Code (Text):
            at net.prickledpvp.empires.PlayerObject.setClan(PlayerObject.java:42) ~[?:?]
    Line 42 is:
    Code (Text):
                if (clanObject.getName().equals(arg))
     
  2. Which error? npe?
     
  3. What is the error you are getting? I know you shows which line it is on,but what is the exact error? I assume it's a NullPointerException, but I want to be sure.
     
  4. I assume this would be a nullpointerexception, because Java. Please let us know wether this is true or not
     
  5. if it's a npe
    the name is null
    or
    the argument is null
     
  6. Yes, nullpointerexception.
    I've confirmed the arg is not null.
     
  7. Either ClanObject is null or getName() returns null.
     
  8. I was not setting clanObject.getName correctly, however now I get this error?
    Code (Text):
     [Empires] Enabling Empires v1.0
    [09:51:53 ERROR]: Cannot load plugins\Empires\ClanObjects\Clan-a07c9bcb-e122-4c4e-aedf-e84e86e64834.yml
    org.bukkit.configuration.InvalidConfigurationException: could not determine a constructor for the tag tag:yaml.org,2002:java.util.UUID
    in 'string', line 2, column 12:
          Creator: !!java.util.UUID '4de829f9-241d- ...
                   ^

            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:56) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:184) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:130) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:179) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.prickledpvp.empires.ClanObject.<init>(ClanObject.java:22) [empires.jar:?]
            at net.prickledpvp.empires.ClanManager.clanSetup(ClanManager.java:56) [empires.jar:?]
            at net.prickledpvp.empires.Main.onEnable(Main.java:31) [empires.jar:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:376) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:326) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:750) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.Bukkit.reload(Bukkit.java:540) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:636) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: org.yaml.snakeyaml.constructor.ConstructorException: could not determine a constructor for the tag tag:yaml.org,2002:java.util.UUID
    in 'string', line 2, column 12:
          Creator: !!java.util.UUID '4de829f9-241d- ...
                   ^

            at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructUndefined.construct(SafeConstructor.java:505) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:373) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:147) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:354) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:489) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:373) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:147) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:354) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:489) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.Yaml.load(Yaml.java:369) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:54) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            ... 22 more
    [09:51:53 ERROR]: Cannot load plugins\Empires\ClanObjects\Clan-c369698b-8fae-4c15-a82c-9ed77fc3e041.yml
    org.bukkit.configuration.InvalidConfigurationException: could not determine a constructor for the tag tag:yaml.org,2002:java.util.UUID
    in 'string', line 2, column 12:
          Creator: !!java.util.UUID '4de829f9-241d- ...
                   ^

            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:56) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:184) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:130) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:179) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.prickledpvp.empires.ClanObject.<init>(ClanObject.java:22) [empires.jar:?]
            at net.prickledpvp.empires.ClanManager.clanSetup(ClanManager.java:56) [empires.jar:?]
            at net.prickledpvp.empires.Main.onEnable(Main.java:31) [empires.jar:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:376) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:326) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:750) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.Bukkit.reload(Bukkit.java:540) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:636) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: org.yaml.snakeyaml.constructor.ConstructorException: could not determine a constructor for the tag tag:yaml.org,2002:java.util.UUID
    in 'string', line 2, column 12:
          Creator: !!java.util.UUID '4de829f9-241d- ...
                   ^

            at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructUndefined.construct(SafeConstructor.java:505) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:373) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:147) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:354) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:489) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:373) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:147) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:354) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:489) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.yaml.snakeyaml.Yaml.load(Yaml.java:369) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:54) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            ... 22 more
    Code (Text):
        public String getName()
        {
            return config.getString("Clan.Name");
        }


    Code (Text):

        /* CONFIG */
        static File file;
        static FileConfiguration config;

    public static void createClan(String Name, Player player)
        {
            String ID = GenerateClanId();
            file = new File("plugins/Empires/ClanObjects", ID + ".yml");
            config = YamlConfiguration.loadConfiguration(file);
         
            config.set("Clan.Creator", player.getUniqueId());
            config.set("Clan.Name", Name);
            config.set("Clan.Id", ID);
            Save();
         
            PlayerObject playerObject = Main.Players.get(player.getName());
            Clans.put(ID, new ClanObject(ID));  
         
            playerObject.setClan(Name);
         
            player.sendMessage("§aYour clan §3" + Name + " §awas successfully created!");
        }

        public static void clanSetup()
        {

            /* If this pathname does not denote a directory, then listFiles() returns null. */
            File[] files = new File("plugins/Empires/ClanObjects").listFiles();

            for (File f : files) {
               if (f.isFile()) {
                  String tId = f.getName().substring(0, f.getName().length() - 4);
                    if (!Clans.containsKey(tId)){
                        Clans.put(tId, new ClanObject(tId));  
                    }        
               }
            }
        }
    /* SAVE */
        public static void Save()
        {
            try {
                config.save(file);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
     
     
  9. Looks like an invalid YAML to me. Check if it contains any tabs or incorrect structure.
     
  10. Code (Text):
    Clan:
      Creator: !!java.util.UUID '4de829f9-241d-4c33-97b0-7c78158aea75'
      Name: '23123'
      Id: Clan-a07c9bcb-e122-4c4e-aedf-e84e86e64834
     
    Interesting, now it makes a bit more sense.

    Any idea why
    Code (Text):
            config.set("Clan.Creator", player.getUniqueId());
    displays as
    Code (Text):
      Creator: !!java.util.UUID '4de829f9-241d-4c33-97b0-7c78158aea75'
    ?
     
  11. It can't convert the UUID to a String. Use this instead:

    Code (Text):
    config.set("Clan.Creator", player.getUniqueId().toString());
     
    • Winner Winner x 1
  12. oh I feel so stupid, Thank you!
     
    • Like Like x 1
  13. Glad I could help :p