Reading from a file

Discussion in 'Plugin Development' started by Logon, Jan 19, 2011.

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

    Logon

    Hello, Im trying to read from a file first i tried putting it in a class that didn't work then i tried putting it like this

    Code:
    public static LinkedList PlayerList = new LinkedList();
       public static void ReadWhitelist()
       {
           PlayerList.clear();
           File dir2 = new File("");
               File fileName = new File(dir2.getAbsolutePath() + "\\whitelist.txt");
                Scanner file;
                try {
                file = new Scanner(fileName);
                String temp = "ttttt";
                while(temp.length() > 0)
                {
                 temp = file.nextLine().toLowerCase();
                 PlayerList.add(temp);
                }
                } catch (FileNotFoundException e) {
                //do something else.
                }
    
       }
    
    This isn't working either i have no idea why when i call the /wl refresh i get this error (happens even if it is in a onlogin /whatever)

    Code:
    2011-jan-19 15:17:45 org.bukkit.plugin.SimplePluginManager callEvent
    ALLVARLIG: Could not pass event PLAYER_COMMAND to Whitelist
    java.util.NoSuchElementException: No line found
            at java.util.Scanner.nextLine(Unknown Source)
            at com.bukkit.Logon.Whitelist.Whitelist.ReadWhitelist(Whitelist.java:60)
    
            at com.bukkit.Logon.Whitelist.WhitelistPlayerListener.onPlayerCommand(Wh
    itelistPlayerListener.java:42)
            at org.bukkit.plugin.java.JavaPluginLoader.callEvent(JavaPluginLoader.ja
    va:134)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:199)
            at net.minecraft.server.NetServerHandler.c(NetServerHandler.java:540)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:521)
            at net.minecraft.server.Packet3Chat.a(SourceFile:24)
            at net.minecraft.server.NetworkManager.a(SourceFile:232)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:63)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:104)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:261)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:197)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:512)
    I have no idea what to do any ideas ?

    Thanks
     
  2. Offline

    Lycake

    I don't have time so i haven't read the whole code yet. Seems like you are trying to read something out of an empty file. The exception you catch is only for file not found and not if it's empty. Check if there is a line
     
  3. Offline

    Plague

    Just a comment on your code, do not use '\\' separators (WIN-only) or '/' separators (*NIX) only. There is a function to call that puts a separator itself:
    Code:
    File.separator
     
  4. Offline

    Logon

    dropped that shit did like this instead

    Code:
    System.out.println("Starting to Readfile");
           PlayerList.clear();
           File dir2 = new File("");
               File fileName = new File("whitelist.txt");
    
                try {
                        FileReader h = new FileReader(fileName);
                        BufferedReader br = new BufferedReader(h);
                        String temp = "ttttt";
                        while(temp.length() > 0)
                        {
                         temp = br.readLine();
                         PlayerList.add(temp);
                         System.out.println("Added user: " + temp);
    
                        }
    
                } catch (FileNotFoundException e) {}
                  catch(IOException h){}
    same error ...
     
  5. Offline

    DerpinLlama

    Can you post your entire .java file?
     
  6. Offline

    Logon

    Fixed it ... You guys should look over the nullpointerexception i think ...
    Because it calls it even tho it still works so if i catch it and just continue on it works flawlessly
     
  7. Offline

    Plague

    Lol, that's not really a fix that's just a hack around it. If you get NullPointerException and do not know where it came from, something is wrong with your code.

    And please do not reply "catching NullPointerException is bad? you suck" or something like that, because I didn't say that.
     
Thread Status:
Not open for further replies.

Share This Page