1.8.8 UUID#fromString YAML getString error

Discussion in 'Spigot Plugin Development' started by notjacob1, Jan 31, 2020.

  1. Basically, when I try to get a UUID from the YML file and convert it to a UUID, which further converts to an offline player, it returns an NPE. I printed out the UUID of the offline player I'm trying to convert to, and it prints the exact thing in the YML.

    Code (Text):

    public Faction(String name) {
        ff = new File("plugins/SysFactions/factions/" + name + ".yml");
        yml = YamlConfiguration.loadConfiguration(ff);
        NAME = name;
        System.out.println(yml.getString("creator"));
        owner = Bukkit.getOfflinePlayer(UUID.fromString(yml.getString("creator")));
        level = (float) yml.getDouble("level");
        playercount = yml.getInt("playercount");
        List<String> ymlplayersraw = yml.getStringList("players");
        ymlplayersraw.stream().forEach(p -> {
            players.add(Bukkit.getOfflinePlayer(UUID.fromString(p)));
        });
        if (level >= 5 && yml.getBoolean("home.set")) {
             home = new Location(Bukkit.getWorld(yml.getString("home.W")), yml.getDouble("home.X"),
             yml.getDouble("home.Y"), yml.getDouble("home.Z"));
        } else {
        home = null;
        }
    }
     
    Code (Text):

    [00:18:33 ERROR]: Error occurred while enabling SysFactions v1.0.0 (Is it up to date?)
    java.lang.NullPointerException
    at java.base/java.util.UUID.fromString(UUID.java:197) ~[?:?]
    at github.notjacobdev.methods.Faction.<init>(Faction.java:77) ~[?:?]
    at github.notjacobdev.handlers.FactionHandler.registerAll(FactionHandler.java:24) ~[?:?]
    at github.notjacobdev.main.FactionsMain.onEnable(FactionsMain.java:38) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    at java.base/java.lang.Thread.run(Thread.java:830) [?:?]
     
    Code (Text):

    name: testfac
    creator: b35754a989d146b9b2c3c87902ce22f5
    level: 0.0
    playercount: 1
    players:
      - dripswag
    home:
      W: '0'
      X: 0.0
      Y: 0.0
      Z: 0.0
      set: false
     
    Any thoughts?
     
  2. How do you save the UUID?
     
  3. Code (Text):
     
    yml.addDefault("creator", creator.getPlayer().getUniqueId().toString().replace("-", ""));
     
     
  4. Why do you replace the "-"?
    I think that is a part of the UUID
    try delete the replace
     
  5. Deleted the replace and also changed it to Player instead of OfflinePlayer, still getting the same error
    the entire class if it matters: https://pastebin.com/cEsXaSUJ
    also the registration method: https://pastebin.com/wheEh0vn
     
  6. drives_a_ford

    Moderator

    What this says:
    Is that there was an NPE within the method. Chances are you passed null to it.
    There likely isn't a creator path in the config. You can add a debug message to print out what yml.getString("creator") returns.

    EDIT:
    Just because you add a default for creator once, doesn't mean it'll magically appear the next time you load the file. The YAML parser can only read what's written in the file. It cannot read your mind. If you want the creator to be saved to the file, you need to set the value. You can set the default if you wish, but you'd need to keep track of the defaults somewhere else, separately, and set it each time a config is loaded.
     
    #6 drives_a_ford, Jan 31, 2020
    Last edited: Jan 31, 2020
  7. You have to set the uuid in ““ (String) in .yml before calling UUID#getFromString()
     
  8. I did that and it changed nothing
    Code (Text):

    File fff = new File("plugins/SysFactions/factions/a.yml");
    YamlConfiguration yml = YamlConfiguration.loadConfiguration(fff);
    System.out.println(yml.getString("creator"));

    [23:21:02 INFO]: b35754a9-89d1-46b9-b2c3-c87902ce22f5
     
    I printed it and it returned the UUID as a string, no null errors there
    Also tried setting the yml as you said and it didn't change anything as far as I can tell

    at this point, I'm probably just going to try a different approach to loading in the player, but thank you guys for your help
     
  9. I’ve been using that method with no problem, idk what’s going on here

    what’s your line 77?
     
  10. Code (Text):
     owner = Bukkit.getPlayer(UUID.fromString(yml.getString("creator")));
     
  11. It returns null if the player isnt online tho
     
  12. You may have solved it, will test when I get home

    EDIT: nope, changing it to Bukkit#getOfflinePlayer returns the same error
     
    #12 notjacob1, Feb 2, 2020
    Last edited: Feb 2, 2020
  13. drives_a_ford

    Moderator

    Again, read the stacktrace. The NPE is within UUID#fromString. So the issue is with what you passed to that method.

    As for setting the value, you also need to save the config using FileConfiguration#save.