Solved getDataFolder is returning null

Discussion in 'BungeeCord Plugin Development' started by KarmaConfigs, Feb 13, 2020.

Thread Status:
Not open for further replies.
  1. I need help, I'm trying to get the plugin folder but if I use getDataFolder() it returns NPE, also if I use getPluginsFolder()
     
  2. The folder isn't there then. You'll have to create the folder before accessing it.
     
  3. I've already done this

    Code (Text):
    if (!this.getDataFolder().exists) {
           this.getDataFolder().mkdir();
    }
    And it keeps returning NPE
     
  4. Do you see the folder in your server files?
     
  5. Yes

    This is the NPE
    Code (Java):
    java.lang.NullPointerException
        at net.md_5.bungee.api.plugin.Plugin.getDataFolder(Plugin.java:61)
        at io.github.karmaconfigs.Bungee.Utils.FilesRelated.FileCreator.<init>(FileCreator.java:61)
        at io.github.karmaconfigs.Bungee.Main.onEnable(Main.java:49)
        at net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:250)
        at net.md_5.bungee.BungeeCord.start(BungeeCord.java:280)
        at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:62)
        at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15)
    Line 49 of Main
    FileCreator config = new FileCreator("config.yml", "", true);

    Line 61 of FileCreator
    this.file = new File( new Main().getDataFolder() + File.separator + fileDir, fileName);
     
    #5 KarmaConfigs, Feb 14, 2020
    Last edited: Feb 14, 2020
  6. Is "this" actually set to anything? It would be helpful if we saw some code.
     
  7. they always ask for help but never show the code
    :C
     
  8. Because someone could copy hours and hours of code
     
  9. Code (Java):

         
    public File file;

         /**
         * Starts the file creator
         *
         * @param fileName the file name
         * @param fileDir the file dir
         * @param isResource if the file is inside the plugin itself
         */

        public FileCreator(@NotNull String fileName, @NotNull String fileDir, boolean isResource) {
            this.fileName = fileName;
            this.fileDir = fileDir;
            this.isResource = isResource;
            this.customResource = false;

            this.file = new File( new Main().getDataFolder() + File.separator + fileDir, fileName);
            this.folder = new File(new Main().PluginFolder() + File.separator + fileDir);
        }
    PluginFolder() calls getDataFolder in Main, I was testing something
     
  10. This is not hours of code...

    Why are you making a new instance of your main class? As far as I know you can't just do it like that. Why not just pass an instance of main to your class? You could also make a static instance so you don't even have to make a constructor for it.
     
  11. Believe it or not, I had it before, but for some reason, when considering the events, bungeecord showed errors related to the folder

    Code (Text):
    01:59:33 [ADVERTENCIA] Error dispatching event PostLoginEvent(player=EdwinCrack) to listener io.github.ka[email protected]
    java.lang.NullPointerException
        at net.md_5.bungee.api.plugin.Plugin.getDataFolder(Plugin.java:61)
        at io.github.karmaconfigs.Bungee.Utils.FilesRelated.FolderMaker.<init>(FolderMaker.java:23)
        at io.github.karmaconfigs.Bungee.Events.JoinEvent.OnJoin(JoinEvent.java:32)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:46)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:400)
        at net.md_5.bungee.connection.InitialHandler$6$1.run(InitialHandler.java:525)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:497)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at java.lang.Thread.run(Unknown Source)
    JoinEvent line 32
    FolderMaker fM = new FolderMaker("Users");

    FolderMaker line 23
    this.file = new File( Main.getInst().getDataFolder() + File.separator + folderName);

    FolderMaker.class
    Code (Java):
    package io.github.karmaconfigs.Bungee.Utils.FilesRelated;

    import io.github.karmaconfigs.Bungee.Main;
    import net.md_5.bungee.api.plugin.Listener;
    import org.jetbrains.annotations.NotNull;

    import java.io.File;

    public class FolderMaker implements Listener {

        public File file;

        /**
         * Starts the file creator
         *
         * @param folderName The folder name
         */

        public FolderMaker(@NotNull String folderName) {
            this.file = new File( Main.getInst().getDataFolder() + File.separator + folderName);
        }

        public void createFolder() {
            if (!file.exists()) {
                file.mkdir();
            }
        }
    }
     
    But there's more (I've checked the full error, and every line of code is correct, without warnings or "new Main().getInst()"
    Code (Java):
    [ADVERTENCIA]
     Error dispatching event ServerConnectEvent(player=EdwinCrack, target=BungeeServerInfo(name=lobby, socketAddress=localhost:25565, restricted=false), cancelled=false, reason=JOIN_PROXY) to listener io.github.karmaconfigs.Bungee.Events.JoinEvent@2e8c1c9b
    java.lang.NullPointerException
        at net.md_5.bungee.api.plugin.Plugin.getDataFolder(Plugin.java:61)
        at io.github.karmaconfigs.Bungee.Main.PluginFolder(Main.java:165)
        at io.github.karmaconfigs.Bungee.Utils.FilesRelated.Config.<init>(Config.java:17)
        at io.github.karmaconfigs.Bungee.Utils.FilesRelated.Messages.<init>(Messages.java:42)
        at io.github.karmaconfigs.Bungee.Events.JoinEvent.onConnect(JoinEvent.java:115)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:46)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:400)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:278)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:268)
        at net.md_5.bungee.connection.InitialHandler$6$1.run(InitialHandler.java:539)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:497)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at java.lang.Thread.run(Unknown Source)
    02:03:51 [ADVERTENCIA] Error dispatching event ServerConnectEvent(player=EdwinCrack, target=BungeeServerInfo(name=lobby, socketAddress=localhost:25565, restricted=false), cancelled=false, reason=JOIN_PROXY) to listener io.github.karmaconfigs.Bungee.Events.JoinEvent@2e8c1c9b
    java.lang.NullPointerException
        at net.md_5.bungee.api.plugin.Plugin.getDataFolder(Plugin.java:61)
        at io.github.karmaconfigs.Bungee.Main.PluginFolder(Main.java:165)
        at io.github.karmaconfigs.Bungee.Utils.FilesRelated.Config.<init>(Config.java:17)
        at io.github.karmaconfigs.Bungee.Utils.FilesRelated.Messages.<init>(Messages.java:24)
        at io.github.karmaconfigs.Bungee.Events.JoinEvent.onConnection(JoinEvent.java:62)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:46)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:400)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:278)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:268)
        at net.md_5.bungee.connection.InitialHandler$6$1.run(InitialHandler.java:539)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:497)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at java.lang.Thread.run(Unknown Source)
    02:03:51 [ADVERTENCIA] Event ServerConnectEvent(player=EdwinCrack, target=BungeeServerInfo(name=lobby, socketAddress=localhost/127.0.0.1:25565, restricted=false), cancelled=false, reason=JOIN_PROXY) took 9.162ms to process!
    02:03:51 [INFORMACIÓN] [EdwinCrack] <-> ServerConnector [lobby] has connected
    02:03:51 [ADVERTENCIA] Error dispatching event ServerConnectedEvent(player=EdwinCrack, server=net.md_5.bungee.ServerConnection@2d0dac1c) to listener io.github.karmaconfigs.Bungee.Events.JoinEvent@2e8c1c9b
    java.lang.NullPointerException
        at io.github.karmaconfigs.Bungee.Events.JoinEvent.onConnect(JoinEvent.java:94)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:46)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:400)
        at net.md_5.bungee.ServerConnector.handle(ServerConnector.java:180)
        at net.md_5.bungee.protocol.packet.Login.handle(Login.java:105)
        at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:104)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:321)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:308)
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:422)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
        at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at java.lang.Thread.run(Unknown Source)
     
    #11 KarmaConfigs, Feb 14, 2020
    Last edited: Feb 14, 2020
  12. md_5

    Administrator Developer

    Code (Text):
            this.file = new File( new Main().getDataFolder() + File.separator + fileDir, fileName);
            this.folder = new File(new Main().PluginFolder() + File.separator + fileDir);
    You can't make a new Main class, you have to use the existing instance
     
  13. Already changed this
    this.file = new File( Main.getInst().getDataFolder() + File.separator + fileDir, fileName);
    this.folder = new File(Main.getInst().PluginFolder() + File.separator + fileDir)

    and keeps not working

    As I said, my old Main class wasn't my actual, I mean, the old Main class was called PluginBungee and the Main class was something like "Utils" class
     
  14. in main class:
    Code (Text):
    public static <class name> getInstance() {
            return JavaPlugin.getPlugin(<class name>.class);
        }
    , in folder make:
    Code (Text):
    this.file = new File( <main class name>.getInstance().getDataFolder() + File.separator + fileDir, fileName);
            this.folder = new File(<main class name>.getInstance().PluginFolder() + File.separator + fileDir);
     
  15. And that's how I have, with the difference of that I can't use JavaPlugin, cause this is BungeeCord

    I have this
    Code (Java):
    public static Main llb;

    public Main() {
            Main.llb = this;
    }

    public static Main getInst() {
            return Main.llb;
    }
     
  16. in the top of your on enable add this:
    Code (Text):
    llb = this;
    That will set llb to your main class instance.
     
  17. This is already done on public Main()


    Code (Text):
    public Main() {
            Main.llb = this;
        }
     
  18. Why not try what I said anyway? :)
     
  19. I already tried it, and it didn't work :c
     
  20. DEPENDENCY INJECTION
    DEPENDENCY INJECTION
    DEPENDENCY INJECTION

    Also when creating dirs use .mkdirs() , not .mkdir()
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.