Solved Help with NullPointerException

Discussion in 'Plugin Development' started by waxman57, Sep 21, 2013.

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

    waxman57

    Code:java
    1. @SuppressWarnings("unchecked")
    2. public <T> T get(String path) {
    3. return (T) arenas.get(path);
    4. }

    It keeps giving me a null pointer exception

    This is my whole code
    Code:java
    1. package me.sambanks57.FreezeTag;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.configuration.ConfigurationSection;
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.plugin.Plugin;
    8.  
    9. public class SettingsManager {
    10.  
    11. private SettingsManager(){
    12.  
    13. }
    14.  
    15. private static SettingsManager instance = new SettingsManager();
    16.  
    17. public static SettingsManager getInstance(){
    18. return instance;
    19. }
    20.  
    21. private FileConfiguration arenas;
    22. private File afile;
    23. private Plugin p;
    24.  
    25. public void setup(Plugin p){
    26. this.p = p;
    27.  
    28. if(!p.getDataFolder().exists()) p.getDataFolder().mkdir();
    29.  
    30. afile = new File(p.getDataFolder(), "arenas.yml");
    31. if(!afile.exists()){
    32. try{
    33. afile.createNewFile();
    34. }
    35. catch(Exception e) {
    36. e.printStackTrace();
    37. }
    38. }
    39. }
    40.  
    41. @SuppressWarnings("unchecked")
    42. public <T> T get(String path) {
    43. return (T) arenas.get(path);
    44. }
    45.  
    46. public void set(String path, Object value){
    47. arenas.set(path, value);
    48. save();
    49. }
    50.  
    51. public ConfigurationSection createConfigurationSection(String path){
    52. ConfigurationSection s =arenas.createSection(path);
    53. save();
    54. return s;
    55. }
    56.  
    57. private void save(){
    58. try{
    59. arenas.save(afile);
    60. }
    61. catch(Exception e){
    62. e.printStackTrace();
    63. }
    64. }
    65.  
    66. public Plugin getPlugin(){
    67. return p;
    68. }
    69. }
    70.  
     
  2. Offline

    Pulsior

    Can you post a stack trace? Stack traces are everything when dealing with exceptions.
     
  3. Offline

    JazzaG

    waxman57

    'arenas' hasn't been initialised
     
  4. Offline

    waxman57

    how would i initialize it? also thx for the reply

    13:14:47 [INFO] [FreezeTag] Enabling FreezeTag v1.1
    13:14:47 [SEVERE] Error occurred while enabling FreezeTag v1.1 (Is it up to date
    ?)
    java.lang.NullPointerException
    at me.sambanks57.FreezeTag.SettingsManager.get(SettingsManager.java:44)
    at me.sambanks57.FreezeTag.ArenaManager.setup(ArenaManager.java:25)
    at me.sambanks57.FreezeTag.FreezeTag.onEnable(FreezeTag.java:18)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.jav
    a:282)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.
    java:264)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.reload(CraftServer.java:60
    5)
    at org.bukkit.Bukkit.reload(Bukkit.java:275)
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchServerCommand(Craf
    tServer.java:512)
    at net.minecraft.server.v1_6_R2.DedicatedServer.ar(DedicatedServer.java:
    262)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    27)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)

    is my error

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  5. Offline

    Cirno

    Add into your constructor:
    arenas = YamlConfiguration.loadFile(afile);
     
  6. Offline

    waxman57

    14:20:50 [SEVERE] Could not load 'plugins\FreezeTag.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:182)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugins(CraftServer.ja
    va:239)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.reload(CraftServer.java:60
    3)
    at org.bukkit.Bukkit.reload(Bukkit.java:275)
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchServerCommand(Craf
    tServer.java:512)
    at net.minecraft.server.v1_6_R2.DedicatedServer.ar(DedicatedServer.java:
    262)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    27)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.ExceptionInInitializerError
    at me.sambanks57.FreezeTag.FreezeTag.<init>(FreezeTag.java:13)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:178)
    ... 14 more
    Caused by: java.lang.IllegalArgumentException: File cannot be null
    at org.apache.commons.lang.Validate.notNull(Validate.java:203)
    at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(Yam
    lConfiguration.java:170)
    at me.sambanks57.FreezeTag.SettingsManager.<init>(SettingsManager.java:2
    4)
    at me.sambanks57.FreezeTag.SettingsManager.<clinit>(SettingsManager.java
    :16)
    ... 20 more
    14:20:50 [INFO] SubClassFactory parent ClassLoader [org.bukkit.plugin.java.Plugi
    nClassLoader]

    Now it gives me that error, also i used arenas = YamlConfiguration.loadConfiguration(afile);
     
  7. Offline

    1Rogue Retired Staff

    the .loadConfiguration(aFile) has to go after you initialize the value, you would be setting this value last...
     
  8. Offline

    waxman57

    So your saying I should declare the variable first, and then initialize it in another statement?

    Please help

    Code:java
    1. return (T) arenas.get(path);

    is giving me the error

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. Offline

    waxman57

    I figured it out
     
Thread Status:
Not open for further replies.

Share This Page