Getting null not sure how to fix

Discussion in 'Plugin Development' started by XxZHALO13Xx, Jan 13, 2015.

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

    XxZHALO13Xx

    So i know this would be null because it doesnt exist hard to explain but ive created a folder called Data and im trying to put a custom .yml file there and it wont create i keep getting null pointers..


    Core
    Code:
     
    private PlayersConfig playersConfig;
    
    public void onEnable() {
    
            createMainFolder();
            createDataFolder();
            playersConfig.createPlayerData(); //Null here
    
    PlayersConfig
    Code:
    
    public class PlayersConfig {
    
        private Core plugin;
    
        public PlayersConfig(Core plugin){
            this.plugin = plugin;
        }
    
        public final File playerData = new File(plugin.getDataFolder() + "/Data/playerData.yml");
        public final FileConfiguration pdata = YamlConfiguration.loadConfiguration(playerData);
    
    
        public void savePlayerData() {
            try {
                pdata.save(playerData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void loadPlayerData() {
            if (playerData.exists()) {
                try {
                    pdata.load(playerData);
                } catch (IOException | InvalidConfigurationException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    pdata.save(playerData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public void createPlayerData(){
            if(!playerData.exists()){
                playerData.mkdir();
            }
        }
    }
     
  2. Offline

    SuperOriginal

    You never instantiated playersConfig.
     
  3. Offline

    teej107

  4. Offline

    ZodiacTheories

  5. Offline

    XxZHALO13Xx

    @teej107 I know how to trouble shoot it :p I just don't understand why its null. Like the file isn't there so ya it is null but if i see if its not null to remove the error then it won't like create.
     
  6. Offline

    sirrus86

    The field playersConfig has nothing assigned to it, so you can't very well work with its methods until you assign something to it. First step would be to assign the field to something, such as a new instance of the PlayersConfig class.
     
  7. Offline

    XxZHALO13Xx

    @sirrus86 So private PlayersConfig playersConfig = new PlayersConfig();
     
  8. Offline

    SuperOriginal

    @XxZHALO13Xx It would actually be new PlayersConfig(this); because you're passing the instance of your main class to the PlayersConfig class through it's constructor.

    That's what this is:
    If you didn't know.
     
  9. Offline

    XxZHALO13Xx

    @SuperOriginal

    Code:
    [12:45:33 ERROR]: Could not load 'plugins\TopRaidzCore.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:131) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:328) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:251) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugins(CraftServer.ja
    va:357) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.<init>(CraftServer.java:31
    9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.PlayerList.<init>(PlayerList.java:68) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.DedicatedPlayerList.<init>(SourceFile:14
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.jav
    a:126) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :436) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
    Caused by: java.lang.NullPointerException
            at me.xxzhalo13xx.topraidzcore.configs.PlayersConfig.<init>(PlayersConfi
    g.java:23) ~[?:?]
            at me.xxzhalo13xx.topraidzcore.Core.<init>(Core.java:37) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ~[?:1.7.0_51]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    ~[?:1.7.0_51]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce) ~[?:1.7.0_51]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_5
    1]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:52) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:127) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            ... 9 more
    Line 37 Core
    Code:
    private PlayersConfig playersConfig = new PlayersConfig(this);
    Enable
    Code:
    public void onEnable() {
    
            createMainFolder();
            createDataFolder();
            playersConfig.createPlayerData();
    
     
  10. Offline

    SuperOriginal

    assign the value to playersConfig variable in your onEnable, not above it. (Keep the variable instantiation there though)
     
  11. Offline

    XxZHALO13Xx

    @SuperOriginal what do u mean? like leave the
    1. private PlayersConfig playersConfig = new PlayersConfig(this);
    2. as it is? then on enable do private PlayersConfig playersConfig;?
     
  12. Offline

    SuperOriginal

    Code:
    private MyOtherClass class;
    public void onEnable(){
      class = new MyOtherClass(parameter);
    }
    There's probably a fancy term to explain it better, but here's an example.
     
  13. Offline

    XxZHALO13Xx

    @SuperOriginal ok so now i have

    Code:
    private PlayersConfig playersConfig;
    
    public void onEnable() {
    
            playersConfig = new PlayersConfig(this);
            createMainFolder();
            createDataFolder();
            playersConfig.createPlayerData();
    }
    and i get

    Code:
    [12:57:36 ERROR]: Error occurred while enabling TopRaidzCore v1.0 (Is it up to d
    ate?)
    java.lang.NullPointerException
            at me.xxzhalo13xx.topraidzcore.configs.PlayersConfig.<init>(PlayersConfi
    g.java:23) ~[?:?]
            at me.xxzhalo13xx.topraidzcore.Core.onEnable(Core.java:45) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:324) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:404) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.jav
    a:448) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.
    java:382) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(CraftServer.java:80
    1) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.Bukkit.reload(Bukkit.java:288) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:703) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(Craf
    tServer.java:690) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:
    296) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    61) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
     
  14. Offline

    SuperOriginal

    Are you sure you're exporting all your classes?
     
  15. Offline

    XxZHALO13Xx

    @SuperOriginal yes

    heres my full enable

    Code:
    public void onEnable() {
    
           playersConfig = new PlayersConfig(this);
            createMainFolder();
            createDataFolder();
           playersConfig.createPlayerData();
    
        if (playersConfig != null) {
                    playersConfig.loadPlayerData();
                }
    EDIT: Full relevant onEnable

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 14, 2015
  16. Offline

    SuperOriginal

    Make the FileConfiguration object a YamlConfiguration, and you don't need the load method.
     
  17. Offline

    XxZHALO13Xx

    @SuperOriginal

    so i changed it to

    public final YamlConfiguration pdata = YamlConfiguration.loadConfiguration(playerData);

    Do i delete the load PlayerData method?
     
  18. Offline

    SuperOriginal

    Yes. It isn't necessary.
     
  19. Offline

    XxZHALO13Xx

    @SuperOriginal

    PlayersConfig
    Code:
    public class PlayersConfig {
    
        private Core plugin;
    
        public PlayersConfig(Core plugin){
            this.plugin = plugin;
        }
    
        public final File playerData = new File(plugin.getDataFolder() + "/Data/playerData.yml");
        public final YamlConfiguration pdata = YamlConfiguration.loadConfiguration(playerData);
    
    
        public void savePlayerData() {
            try {
                pdata.save(playerData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
     
       
    
        public void createPlayerData(){
            if(!playerData.exists()){
                playerData.mkdir();
            }
        }
    }
    Error
    Code:
    [13:58:11 ERROR]: Error occurred while enabling TopRaidzCore v1.0 (Is it up to d
    ate?)
    java.lang.NullPointerException
            at me.xxzhalo13xx.topraidzcore.configs.PlayersConfig.<init>(PlayersConfi
    g.java:23) ~[?:?]
            at me.xxzhalo13xx.topraidzcore.Core.onEnable(Core.java:45) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:324) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:404) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.jav
    a:448) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.
    java:382) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(CraftServer.java:80
    1) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.Bukkit.reload(Bukkit.java:288) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:703) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(Craf
    tServer.java:690) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:
    296) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    61) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
    Core
    Code:
    private PlayersConfig playersConfig;
    
    public void onEnable() {
    
            playersConfig = new PlayersConfig(this);
            createMainFolder();
            createDataFolder();
            playersConfig.createPlayerData();
    }
     
  20. Offline

    SuperOriginal

    What is line 23 of PlayersConfig?
     
  21. Offline

    XxZHALO13Xx

    public final File playerData = new File(plugin.getDataFolder() + "/Data/playerData.yml");

    @SuperOriginal /\

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 14, 2015
  22. Offline

    Konato_K

    @XxZHALO13Xx plugin in PlayerConfigs is null because the variables you assign on their declaration are done before the constructor is invoked, so the value of plugin is not yet assigned, causing a NPE

    Just move the initialization of those variables in the constructor.
     
  23. Offline

    XxZHALO13Xx

    @Konato_K which ones? the files to the constructor?

    @Konato_K I'm confused... what do u mean

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 14, 2015
  24. Offline

    teej107

    @XxZHALO13Xx Scrap your code and try a different approach. You said you know how to solve NPEs but you obviously don't know how to solve this one.
     
  25. Offline

    timtower Administrator Administrator Moderator

    @XxZHALO13Xx Please stop double posting. Use the edit button instead, it is located next to the date.
     
    teej107 likes this.
  26. Offline

    XxZHALO13Xx

    @teej107 most null pointers are because i didn't do a null check.. i've moved the constructor around and i dont know what he meant but intialize in the constructor. could u explain what he means rather than say scrap my code?

    And @timtower ight sorryz

    EDIT:
    @teej107 I got it working.. the only thing is it creates a folder not a yml file. What do i need to change to make it a .yml
     
  27. Offline

    teej107

    @XxZHALO13Xx Depending on the situation, doing a null check will prevent your application from doing its intended purpose. "plugin" is null and you are trying to use a method from it.
     
  28. Offline

    Skionz

    @XxZHALO13Xx File#mkdir makes a directory. Use File#createNewFile to create a file.
     
  29. Offline

    XxZHALO13Xx

    @teej107


    Code:java
    1. private Core plugin;
    2. public final File playerData;
    3. public final YamlConfiguration pdata;
    4. public PlayersConfig(Core plugin) {
    5. this.plugin = plugin;
    6. playerData = new File(plugin.getDataFolder() + "/Data/playerData.yml");
    7. pdata = YamlConfiguration.loadConfiguration(playerData);
    8. }
    9.  


    Why doesn't it create a .yml file? it makes a folder literally called playerData.yml

    EDIT: ok thanks @Skionz

    EDIT2: Thank y'all so much! I got it generating. Last question. How would i make a config generate everytime a player joins using their name/uuid. not sure which one i should use. i know i need to use playerjoinevent but not sure how to set the file to generate with different things like for example.. file name: XxZHALO13Xx
    Stats for XxZHALO13Xx //Comment
    kills: 0
    deaths: 0
     
  30. Offline

    Skionz

    @XxZHALO13Xx Name the file Player#getName() and add the file extension using concatenation or a StringBuilder.
     
Thread Status:
Not open for further replies.

Share This Page