Custom YAML file error

Discussion in 'Plugin Development' started by tacticalsk8er, Feb 7, 2013.

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

    tacticalsk8er

    OK I have no clue what is wrong with my code, but on the start up of my test server I get this stack trace:
    Stack Trace (open)
    2013-02-07 22:17:50 [SEVERE] Error occurred while enabling Strike v0.1 (Is it up to date?)
    java.lang.NullPointerException
    at me.tacticalsk8er.Strike.YAML.saveDefaultConfig(YAML.java:66)
    at me.tacticalsk8er.Strike.Main.onEnable(Main.java:17)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    at org.bukkit.craftbukkit.v1_4_R1.CraftServer.loadPlugin(CraftServer.java:282)
    at org.bukkit.craftbukkit.v1_4_R1.CraftServer.enablePlugins(CraftServer.java:264)
    at org.bukkit.craftbukkit.v1_4_R1.CraftServer.reload(CraftServer.java:596)
    at org.bukkit.Bukkit.reload(Bukkit.java:184)
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchCommand(CraftServer.java:514)
    at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchServerCommand(CraftServer.java:506)
    at net.minecraft.server.v1_4_R1.DedicatedServer.al(DedicatedServer.java:260)
    at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
    at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
    at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)


    Here is the link to my source code: https://github.com/tacticalsk8er/Strike
    Any advice or suggestions would be appreciated :) Thank you!
     
  2. I can see what's wrong and you should really learn to go through your program using the information from the stack trace.
    Code:
    database = new YAML(this, "Database.yml");
    This is called which calls the YAML constructor.
    Code:
    public YAML(JavaPlugin plugin, String fileName) {
            if (plugin == null)
                throw new IllegalArgumentException("plugin cannot be null");
            if (!plugin.isInitialized())
                throw new IllegalArgumentException("plugin must be initiaized");
            this.plugin = plugin;
            this.fileName = fileName;
        }
    The Constructor finishes but hasn't created or set any useful variables. It hasn't checked if a file exists. Basically, it's done nothing very useful.
    Code:
    database.saveDefaultConfig();
    Then this is called as you're trying to save your config.
    Code:
    public void saveDefaultConfig() {
            if (!configFile[B].exists()[/B]) { 
                this.plugin.saveResource(fileName, false);
            }
        }
    Then you call the .exists method of the configFile variable. configFile is null, you haven't set anything here.

    So:
    1. In the YAML constructor you need to add the file checking and creation method calls.
    2. You should almost always check if a variable is null before you try calling any methods.
     
Thread Status:
Not open for further replies.

Share This Page