NullPointerException?

Discussion in 'Plugin Development' started by knoxcorner, Nov 5, 2011.

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

    knoxcorner

    Okay, so for one of my plugins I'm trying to get it to read from a text file where lines that start with "//" are ignored, ";" indicate that it is a world, and "*" indicates the end of the list. Here's the text file it will read from:

    Code:
    //Any world names found in this file will be excluded from the backup of WorldSaver
    //There should only be lines that start with a ';' followed by the name of the world, '//' to ignore what is on that line,
    //or '*' to mark the end of the list. If it starts with anything else or is an empty line (highlight to see), an error will be returned
    //To exclude a world, type it's name EXACTLY as it is in the folder and start with a ';'
    //At the end of the list, put  a '*' on the next line
    //You may now put world names, do not skip ANY lines
    //----------------------------------
    ;ExampleWorld
    ;hello
    ;worldhere
    *
    //There should be nothing past this. If there is, it will not be read.
    
    and here is the code to do so:
    Code:java
    1. if (excludeworlds.exists() == true) {
    2. try {
    3. BufferedReader exwis = new BufferedReader(new FileReader(excludeworlds));
    4. Boolean finished = false;
    5. int tempint4 = 0;
    6. String tempstring = "";
    7. while (!finished) {
    8. tempstring = exwis.readLine();
    9. if (tempstring.startsWith("//")) {}
    10. else if (tempstring.startsWith(";")) {worldlist[tempint4] = tempstring.replaceAll(";", ""); tempint4++;}
    11. else if (tempstring.startsWith("*")) {printWorlds(); finished = true;}
    12. }
    13.  
    14.  
    15. } catch (IOException ioe) {System.out.println("[WorldSaver] Caught IOException " + ioe + " while trying to write exludeworlds");
    16. }


    It is returning the NullPointerException error on the "else if (tempstring.startsWith(";"))..."
    Any help is appreciated, thanks.
     
  2. Offline

    DDoS

    Why are you making your life so complicated? You don't need to create a new file format... Bukkit comes with YAML support integrated, they even supply they're own API. I know there's a tutorial on it in the Resources sub forum for this forum. I suggest you start there.

    Edit: here's the link: http://forums.bukkit.org/threads/tu...ration-api-create-a-yaml-configuration.42775/

    Here's a example of YAML file for your plugin:

    PHP:
    ##Comments here
    ##And why not here?
    ##-----------------------
    Worlds_To_Exclude:
    ExampleWorld
    hello
    worldhere
     
  3. You are trying to read lines from the file forever, without checking if the file ended, in which case readLine() will return null (hence the NPE).

    Add this before your checks:
    Code:
    if(tempstring == null)
        break;
    Also, replaceAll is probably not what you are looking for. It does the same as "replace", but with regular expressions (google that if you wanna know what that is). I don't think you want that, since you use fixed chars. "replace" will actually replace all, methods are named a bit crappy there.
     
  4. Offline

    knoxcorner

    @DDoS I tried using it, but get the following error:

    Code:
    WorldSaver v1.3 (Is it up to date?): org.bukkit.plugin.Plugin.getConfig()Lorg/bukkit/configuration/file/FileConfiguration;
    java.lang.NoSuchMethodError: org.bukkit.plugin.Plugin.getConfig()Lorg/bukkit/configuration/file/FileConfiguration;
    at servm.knoxcorner.worldsaver.worldSaver.loadConfiguration(worldSaver.java:319)
    at servm.knoxcorner.worldsaver.worldSaver.onEnable(worldSaver.java:344)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:920)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:278)
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:173)
    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:156)
    at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    with this code
    Code:java
    1. templist = worldsaver.getConfig().getList("WorldSaver.Exclude_Worlds");
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    @knoxcorner
    What version Craftbukkit do you have, probably needs an update.
     
  6. Offline

    knoxcorner

    @Sagacious_Zed EDIT:
    *Facepalm*, I had updated the Library but not the server I tested on.
     
Thread Status:
Not open for further replies.

Share This Page