Issues with YMLs

Discussion in 'Plugin Development' started by Rellac, Oct 19, 2014.

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

    Rellac

    So I am attempting to create a YML system within the plugin I'm creating and I'm coming across issues.

    The layout of the yml should be as such:
    Code:
        players:
            <name>
                classes:
                    <class>
                        <date>
                    <class>
                        <date>
            <name>
                classes:
                    <class>
                        <date>
                    <class>
                        <date>
    etc
    
    My initial attempt involved the following layout from my HelloWorld project (which contains a bunch of coding for reference):
    Code:
            if(cmd.getName().equalsIgnoreCase("HelloWorldConfig"))
            {
                YamlConfiguration yaml = new YamlConfiguration();
                yaml.createSection("test");
                yaml.createSection("test.one");
                yaml.createSection("test.two");
                List<String> values = new ArrayList<String>();
                values.add("hello");
                values.add("goodbye");
                values.add("test");
                yaml.set("test.one", values);
                try
                {
                    yaml.save("HelloWorld/HelloWorld.yml");
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
    This system worked pretty well, only it would overwrite the config each time, which was a conundrum.

    I moved on to another system, which would hopefully load/save appropriately:
    Code:
    public class Ranks extends JavaPlugin
    {
        // Load Configs
        private FileConfiguration Players = null;
        private File PlayersFile = null;
     
     
        public void onEnable()
        {
            // Load Player Listings
            getPlayers();
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            // Get Date
            Date now = new Date();
            getPlayers();
     
            // args[0] = player name
            // args[1] = class name
            if(cmd.getName().equalsIgnoreCase("Rank") && args.length == 2)
            {
                Players.createSection("players."+args[0]);
                Players.createSection("players."+args[0]+".classes");
                Players.createSection("players."+args[0]+".classes."+args[1]);
                Players.createSection("players."+args[0]+".classes."+args[1]+".date");
                //players.<player name>.classes.<class name>.date
     
                Players.set("players."+args[0]+".classes."+args[1]+".date", now);
     
     
                getLogger().info("Saving to YML...");
                savePlayers();
                getLogger().info("..Done!");
     
            }
            return false;
        }
     
        // Player File Methods
        //---------------------
     
        // Reload Player Listings
        public void reloadPlayers()
        {
            Reader defConfigStream = null;
            if (Players == null)
            {
                PlayersFile = new File(getDataFolder(), "plugins/Ranks/players.yml");
                Players.createSection("players");
            }
            Players = YamlConfiguration.loadConfiguration(PlayersFile);
     
            // Look for defaults in the jar
     
            try
            {
                defConfigStream = new InputStreamReader(this.getResource("plugins/Ranks/players.yml"), "UTF8");
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
            if (defConfigStream != null)
            {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                Players.setDefaults(defConfig);
            }
        }
     
        // Get Player Listings
        public FileConfiguration getPlayers()
        {
            if (Players == null)
            {
                reloadPlayers();
            }
            return Players;
        }
     
        // Save Player Listings
        public void savePlayers()
        {
            if (Players == null || PlayersFile == null)
            {
                return;
            }
            try
            {
                getPlayers().save(PlayersFile);
            }
            catch (IOException ex)
            {
                getLogger().log(Level.SEVERE, "Could not save config to " + PlayersFile, ex);
            }
        }
    }
    This, however, gave me much more trouble. On loading the server, I would get the following errors thrown at me:
    Code:
    [23:06:32] [Server thread/INFO]: [Ranks] Enabling Ranks v1.0
    [23:06:32] [Server thread/ERROR]: Error occurred while enabling Ranks v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at com.Test.Ranks.Ranks.reloadPlayers(Ranks.java:96) ~[?:?]
        at com.Test.Ranks.Ranks.getPlayers(Ranks.java:122) ~[?:?]
        at com.Test.Ranks.Ranks.onEnable(Ranks.java:29) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:446) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:380) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.n(MinecraftServer.java:352) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.g(MinecraftServer.java:326) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.a(MinecraftServer.java:282) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:182) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    [23:06:32] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [23:06:33] [Server thread/INFO]: Done (22.554s)! For help, type "help" or "?"
    [23:07:45] [Thread-4/INFO]: Stopping server
    [23:07:42] [Server console handler/ERROR]: Exception handling console input
    java.io.IOException: Input/output error
        at java.io.FileInputStream.readBytes(Native Method) ~[?:1.7.0_65]
        at java.io.FileInputStream.read(FileInputStream.java:272) ~[?:1.7.0_65]
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) ~[?:1.7.0_65]
        at java.io.BufferedInputStream.read(BufferedInputStream.java:254) ~[?:1.7.0_65]
        at java.io.FilterInputStream.read(FilterInputStream.java:83) ~[?:1.7.0_65]
        at org.bukkit.craftbukkit.libs.jline.console.ConsoleReader$1.read(ConsoleReader.java:167) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.libs.jline.internal.InputStreamReader.read(InputStreamReader.java:267) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.libs.jline.internal.InputStreamReader.read(InputStreamReader.java:204) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.libs.jline.console.ConsoleReader.readCharacter(ConsoleReader.java:995) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.libs.jline.console.ConsoleReader.readLine(ConsoleReader.java:1167) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadCommandReader.run(ThreadCommandReader.java:32) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    I'm not entirely sure what the issue is here and some help would be very much appreciated.

    I understand that there are already a bunch of permissions plugins, this is mostly a learning experience.
     
  2. Rellac likes this.
  3. [1635] <Literallie> Rellac, you're checking if ´Players´ is null and then accessing it anyways. That's your error.
    [1635] <Literallie> Rellac, also, please try naming your fields in camelCase, makes it easier to read.
    [1636] <Rellac> you mean within getPlayers?
    [1636] <Literallie> The stack trace even tells you the exact line where your problem is located
    [1636] <Literallie> java.lang.NullPointerException
    [1636] <Literallie> at com.Test.Ranks.Ranks.reloadPlayers(Ranks.java:96) ~[?:?]
    [1637] <Rellac> I don't understand, from what I can see, this should check if the Players=null, load up the file that contains them?
    [1637] <Literallie> You're creating a File object in another field.
    [1637] <Literallie> That doesn't affect `Players` at all.
    [1638] <Literallie> I would check whether PlayersFile is null and load it then
    [1638] <Literallie> Then, outside the if block, after initialising `Players`, I would check if the section exists and crete it if it doesn't.
    [1647] <Rellac> Literallie, would you mind perhaps posting this on the thread? I'm actually working on this with a few others and it would be helpful if we could all see the same notes
    [1648] <Literallie> Rellac, aw, I had a post written half-way, then saw you're still on IRC and then deleted it :c
    [1648] <Literallie> I can post what I said on IRC
    [1653] <Rellac> haha, thanks, you're a star
     
    Rellac likes this.
  4. Offline

    Rellac

    So I'm still a tad confused, I seem to have the same issue, even with the amendment you suggested.

    I chopped down some of my code and now I just have the methods

    Code:
        // Reload Player Listings
        public void reloadPlayers()
        {
            Reader defConfigStream = null;
            if (Players == null)
            {
                PlayersFile = new File(getDataFolder(), "plugins/Ranks/players.yml");
            }
            Players = YamlConfiguration.loadConfiguration(PlayersFile);
     
            // Look for defaults in the jar
       
            try
            {
                defConfigStream = new InputStreamReader(this.getResource("plugins/AltumTempRanks/players.yml"), "UTF8");
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
            if (defConfigStream != null)
            {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                Players.setDefaults(defConfig);
            }
        }
     
        // Get Player Listings
        public FileConfiguration getPlayers()
        {
            if (Players == null)
            {
                reloadPlayers();
            }
            return Players;
        }
    Trying to run getPlayers() within onEnable still ouputs
    Code:
    [17:17:50] [Server thread/INFO]: [Ranks] Enabling Ranks v1.0
    [17:17:50] [Server thread/ERROR]: Error occurred while enabling Ranks v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:78) ~[?:1.7.0_65]
        at java.io.InputStreamReader.<init>(InputStreamReader.java:97) ~[?:1.7.0_65]
        at com.Test.Ranks.Ranks.reloadPlayers(Ranks.java:95) ~[?:?]
        at com.Test.Ranks.Ranks.getPlayers(Ranks.java:115) ~[?:?]
        at com.Test.Ranks.Ranks.onEnable(Ranks.java:29) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:446) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:380) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.n(MinecraftServer.java:352) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.g(MinecraftServer.java:326) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.a(MinecraftServer.java:282) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:182) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    I'm not sure where I can see where a null pointer might appear within reloadPlayers() - obviously I'm missing something, but it all seems fine to me
     
  5. Code:
    if (Players == null)
    // should be
    if (PlayersFile == null)
    Also, make sure that you really have a file in your JAR at
    plugins/AltumTempRanks/players.yml

    You can check this by opening your .jar with an archive viewer like 7zip
    .
     
    Rellac likes this.
  6. Offline

    Rellac

    Ah, thanks a lot. I can see where I've gone wrong now. You're a superstar.
     
Thread Status:
Not open for further replies.

Share This Page