yml file issues

Discussion in 'Plugin Development' started by Tekk, Jan 14, 2017.

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

    Tekk

    Hello Guys

    Some days ago I started programming Bukkit 1.9 Plugins, but I just dont't understand how yml files work...
    I've checked many other posts but I just don't get it... :(

    I use
    Code:
    File yml = new File("plugins/tickets.yml");
          if (!yml.exists()) {
              try {
                   yml.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    every time I load the plugin, is this right or should I do this different?

    Anyway, the code above works, but how do I write something to this file now?
    I,ve tried
    Code:
    plugin.getConfig().set("the.location", "My message")
    but this won't write anything into my file... :/

    Thanks,
    Tekk
     
  2. Offline

    RelicObject

    Code:
          File yml = new File(getDataFolder(), "tickets.yml");
          if (!yml.exists()) {
              try {
                   yml.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            YamlConfiguration ticketsYml= YamlConfiguration.loadConfiguration(yml);
            ticketsYml.set("path.to.result", "HI");
            ticketsYml.save(yml);
    
    Let me know if this works/helps
     
  3. Offline

    Tekk

    Works better than my code, but there is still an error in the first line
    Code:
    File yml = new File(getDataFolder(), "tickets.yml");
    by getDataFolder:
    The method getDataFolder() is undefined for the type ticketExecutor
    May this error appear because of this part of my code is in the command executor class?
     
  4. Offline

    JanTuck

    Pass the plugin instance and get from there. The JavaPlugin class.

    Sendt fra min ALE-L21 med Tapatalk
     
  5. Offline

    Tekk

    Thank you, works now :)
    If someone other needs this:
    Code:
    public nameOfMainClass(Plugin plugin) {
            this.plugin = plugin;
        }
    Still have an issue...

    Here is my code:
    Code:
    // get senders name
                String name = sender.getName();
    
                // create tickets.yml if file don't exists
                File yml = new File(plugin.getDataFolder(), "plugins/tickets.yml");
                if (!yml.exists()) {
                    try {
                        yml.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
               // fill tickets.yml
                YamlConfiguration ticketsYml = YamlConfiguration.loadConfiguration(yml);
                ticketsYml.set(name + ".title", parts[1]);
                try {
                    ticketsYml.save(yml);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    Error code in console:
    Code:
    [16:27:20 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tick
    et' in plugin easyTicket v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1350) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java
    :1185) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_111]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9
    .jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7
    21) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6
    60) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java
    :559) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
            at ch.tekk.easyTicketExecutor.onCommand(easyTicketExecutor.java:53) ~[?:
    ?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            ... 15 more
    >
    Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 14, 2017
  6. yes:
    something is null on this line
     
    Zombie_Striker and JanTuck like this.
  7. Offline

    RelicObject

    Assuming this is line #53
    Code:
                ticketsYml.set(name + ".title", parts[1]);
    
    1. It could be the variable "name" is not set. Double check that at some point during the codes execution, this gets set.
    2. the CommandSender did not specify a second argument.
    Reminder: Arrays start at 0, not 1. To access the first(and sometime's only) object in an array, you need to use array[0].
     
  8. Offline

    JanTuck

    If this is solved please mark it as sich. If not please give some more details on the error. What on line 53 in easyTicketExecutor.java?

    Sendt fra min ALE-L21 med Tapatalk
     
  9. This cannot cause a NPE, except when ticketsYml is null, but it shouldn't if the file was created successfully (or is already there), the error must be somewhere else.

    1. Would just result in: null.title
    2. Would throw ArrayIndexOutOfBoundsException

    @Tekk Please post this line or we can't help you with that.
    Or even better: help yourself by learning how to troubleshoot errors (look at sticky post in plugin development forum)
     
    JanTuck likes this.
  10. Offline

    JanTuck

    If i remember correctly getdatafolder returns ~/plugins/PluginName?

    Sendt fra min ALE-L21 med Tapatalk
     
  11. Offline

    RelicObject

    Woke up like 10 minute's before writing that reply, my bad xD

    Only thing i could think of is
    Code:
                File yml = new File(plugin.getDataFolder(), "plugins/tickets.yml");
    
    need's to be
    Code:
                File yml = new File(plugin.getDataFolder(), "tickets.yml");
    
    Maybe because the /plugins/ dir doesnt exist, as plugin.getDataFolder() already directs them to the plugins/<plugin> folder
     
    FisheyLP and Zombie_Striker like this.
  12. Offline

    Tekk

    Hello,
    Line 53 is
    Code:
    File yml = new File(plugin.getDataFolder(), "plugins/tickets.yml");
    so I canged it do
    Code:
    File yml = new File(plugin.getDataFolder(), "tickets.yml");
    but I get the same result...
    And there is no tickes.yml file. I have no idea why this won't work anymore...

    This code is correct to create a new file, isn't it?
    Code:
    File yml = new File(plugin.getDataFolder(), "tickets.yml");
                if (!yml.exists()) {
                    try {
                        yml.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
     
  13. Offline

    JanTuck

    It is.

    The file is not null i think you dont pass the plugin correctly.
     
  14. Offline

    RelicObject

    Are you accessing the plugin object correctly?

    In your main JavaPlugin class, add this:
    Code:
    public static JavaPlugin plugin;
    then in your onEnable, add this:
    Code:
    plugin = this;
    then, when loading config's from other classes, use:
    Code:
    File yml = new File(<main class name>.plugin.getDataFolder(), "tickets.yml");
    
    If you are trying to make the new file within the JavaPlugin class, you can just use:
    Code:
    File yml = new File(getDataFolder(), "tickets.yml");
     
    Tekk likes this.
  15. Offline

    Zombie_Striker

    @RelicObject
    Do not promote static abuse. Unless you know that the other member knows how to manage static objects (and even then, you don't need statics for this), do not recommend it. Insteads, he should pass the main instance through the other class's constructor.
     
    RelicObject likes this.
  16. Offline

    RelicObject

    Like Zombie said, i also wouldnt recommend permanently using the code that i used. More as a test to make sure that wasnt the problem, use constructor arguments to pass the javaplugin object as suggested above.
     
    Zombie_Striker likes this.
  17. Offline

    Tekk

    Thank you guys for you nice support, I tried the answere from RelicObject and it works perfectly now :)
    But what do you mean with "Do not promote static abuse"?
     
Thread Status:
Not open for further replies.

Share This Page