Solved Creating a File in the Plugin's Datafolder

Discussion in 'Plugin Development' started by KeybordPiano459, Dec 29, 2012.

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

    KeybordPiano459

    I'm not getting why this isn't working:
    Code:java
    1. File testFile == null;
    2. if (testFile == null) {
    3. File testFile = new File(plugin.getDataFolder(), "test.txt"); // <-- this one
    4. try {
    5. testFile.createNewFile();
    6. } catch (IOException e) {
    7. e.printStackTrace();
    8. }
    9. }

    I'm getting an NPE on the commented line, but honestly, what there should I put a null check for?

    Just realized, the first line should only have one equals sign :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    fireblast709

    plugin is null maybe
     
  3. Offline

    KeybordPiano459

    Plugin is working in all of my other classes that all happen to have the same constructor =/ Is there another way to generate a file that would work? I just need to make the file visible and editable in my plugin's folder.
     
  4. Offline

    fireblast709

    Post the stacktrace
     
  5. Offline

    KeybordPiano459

  6. Offline

    tommycake50

    the datafolder is null.
    make this check
    File f = getDataFolder();
    if(!f.exists()){
    f.mkdir();
    }
     
  7. Offline

    KeybordPiano459

    No, it's there...
     
  8. Offline

    tommycake50

    whats plugin?
     
  9. Offline

    fireblast709

    post thisClass please :3
     
  10. Offline

    tommycake50

    yeah that would help :3
     
  11. Offline

    fireblast709

    f.mkdir() would not solve a NPE
     
  12. Offline

    tommycake50

    i thought the datafolder was null not the plugin.
    if it was the datafolder it would solve the npe but it's probs the pluin variable.
     
  13. Offline

    KeybordPiano459

    fireblast709
    tommycake50
    http://pastebin.com/6xhn3rLF

    Also, the line that's in my main class that is in it is the one calling the method in the class posted above ^^

    EDIT: Wow I've never used so many two letter words in a row ^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  14. Offline

    tommycake50

  15. Offline

    fireblast709

    No if getDataFolder() would return null, calling .mkdir() would just give you another NPE :p
     
  16. Offline

    KeybordPiano459

    I just need it to be able to use "plugin" for plugin.getDataFolder()
     
  17. Offline

    tommycake50

    oh yeah LOL.

    yeah but can we see your other class where you instantiate thisclass.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  18. Offline

    fireblast709

    Besides that, please refrain from using static. You probably call the method without instantiating an instance of the class
     
  19. Offline

    KeybordPiano459

    Do you need the entire thing? Here's the onEnable()
    Code:java
    1. public void onEnable() {
    2. getLogger().info("thisPlugin v1.0 has been enabled!");
    3. myCommandsClass.getCommands();
    4. anotherMethodGettingCalled();
    5. thisClass.createFile(); // <==
    6.  
    7. try {
    8. BukkitMetrics metrics = new BukkitMetrics(this);
    9. metrics.start();
    10. } catch (IOException e) {
    11. // Failed to submit the stats :-(
    12. }
    13. }
     
  20. Offline

    tommycake50

    do you even instantiate the class with the plugin variable in?
     
  21. Offline

    KeybordPiano459

    fireblast709
    Called it without the static void, and it worked. Thanks :)

    For anyone venturing down this trail like I did, change the top line to the bottom two:
    Code:java
    1. thisClass.createFile();
    2.  
    3. thisClass class = new thisClass(this);
    4. class.createFile();


    Hehehe... awkward... fireblast709 why isn't this working?
    Code:java
    1. if (getDataFolder() == null) {
    2. File file = new File("kEssentials");
    3. file.mkdirs();
    4. }
    It's directly in my onEnable() method.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  22. Offline

    fireblast709

    KeybordPiano459 you know that you are creating <server dir>/kEssentials?
     
  23. Offline

    KeybordPiano459

    Isn't getDataFolder() for the plugin's folder in the plugins directory? Either way, there isn't a folder called kEssentials in the server directory.
     
  24. Offline

    Th3Controller

    Here's an example from my unsuccessful plugin back then.
    Code:
        public void onEnable(){
            createFiles();
            getServer().getPluginManager().registerEvents(new LiveBanListener(), this);
            log.info(this + " is now enabled! Banning system is online!");
        }
       
        public void createFiles(){
            File folder = new File("plugins/Liveban");
            File file = new File("plugins/Liveban/banned.lcfe");
            File file2 = new File("plugins/Liveban/tempbanned.lcfe");
            if(!folder.exists()){
                folder.mkdir();
            }
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(!file2.exists()){
                try {
                    file2.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
     
  25. Offline

    KeybordPiano459

    Th3Controller
    That's pretty much exactly... what... I... did?

    EDIT: Thanks Th3Controller :)
    I changed it to this and it worked
    Code:java
    1. File file = new File("plugins/kEssentials");
    2. file.mkdir();
     
  26. Offline

    Th3Controller

    From what it looks like it's not, do you mind sending the onEnable and the file creator?

    Never mind lol, see it helped :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  27. Offline

    fireblast709

    Code:java
    1. if(!getDataFolder().exists() || !getDataFolder().isDirectory())
    2. {
    3. getDataFolder().mkdirs();
    4. }
    5. File file = new File(getDataFolder(), "some.file");
    6. if(!file.exists())
    7. {
    8. try
    9. {
    10. if(!file.createNewFile())
    11. {
    12. throw new IOException("Failed to create file somehow...");
    13. }
    14. }
    15. catch(IOException ex)
    16. {
    17. ex.printStackTrace();
    18. }
    19. }
    (and no, this is not the same code as Th3Controller )
     
  28. Offline

    KeybordPiano459

    omg... fireblast709 I keep finding errors with this, why am I getting an NPE on the line commented below?
    Code:java
    1. public static FileConfiguration fileConfiguration = null;
    2. public static File theFile = null;
    3. public static void reloadFile() {
    4. if (!theFile.exists()) { // <-- this one
    5. theFile = new File(plugin.getDataFolder(), "file.yml");
    6. }
    7. fileConfiguration = YamlConfiguration.loadConfiguration(theFile);
    8.  
    9. InputStream defConfigStream = plugin.getResource("file.yml");
    10. if (defConfigStream != null) {
    11. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    12. fileConfiguration.setDefaults(defConfig);
    13. }
    14. }
     
  29. Offline

    fireblast709

    Because theFile is null... Where do you define it?
     
  30. Offline

    KeybordPiano459

    fireblast709
    Well the thing is, if I changed the line that defines the file (public static File theFile = null) to this:
    Code:java
    1. public static File theFile = new File(plugin.getDataFolder(), "file.yml");

    I get an NPE on that line (^)

    Even then, the lines that calls the method in my onEnable are these-


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page