Solved NullPointerException, Help me

Discussion in 'Plugin Development' started by Kassestral, May 20, 2014.

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

    Kassestral

    How do I sort out the NullPointerException? I am stuck and have been trying to work it out for about an hour or two >,<
    Code:
    Caused by: java.lang.NullPointerException
        at Kassestral.Levels.Handlers.onPlayerJoin(Handlers.java:36) ~[?:?]
    Levels(Main Class)
    Code:java
    1. package Kassestral.Levels;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.plugin.PluginManager;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Levels extends JavaPlugin
    10. {
    11. PluginManager pm = Bukkit.getServer().getPluginManager();;
    12. public void onEnable()
    13. {
    14. CreateDirectories();
    15. pm.registerEvents(new API(this), this);
    16. pm.registerEvents(new Handlers(this), this);
    17. }
    18.  
    19. public void onDisable()
    20. {
    21.  
    22. }
    23.  
    24. private void CreateDirectories()
    25. {
    26. // Creates Main Directory
    27. File MainDir = new File(getDataFolder() + "/");
    28. if(!MainDir.exists())
    29. {
    30. MainDir.mkdir();
    31. }
    32. // Creates Main Directory
    33.  
    34. // Creates Players Directory
    35. File PlayersDir = new File(getDataFolder() + "/" + File.separator + "players");
    36. if(!PlayersDir.exists())
    37. {
    38. PlayersDir.mkdir();
    39. getLogger().info("Folders Created");
    40. }
    41. // Creates Players Directory
    42. }
    43.  
    44.  
    45. }

    API.class
    Code:java
    1. package Kassestral.Levels;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.Plugin;
    11.  
    12. public class API implements Listener
    13. {
    14. FileConfiguration PlayerConfig;
    15. File PlayerFile;
    16. Player player;
    17.  
    18. private static API instance;
    19. private final Plugin Levels;
    20.  
    21. public API(Plugin Levels)
    22. {
    23. this.Levels = Levels;
    24. }
    25.  
    26. public void LPlayer(Player p)
    27. {
    28. this.player = p;
    29. this.PlayerFile = new File(Levels.getDataFolder() + "/" + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    30. this.PlayerConfig = YamlConfiguration.loadConfiguration(this.PlayerFile);
    31. }
    32.  
    33. public Player getPlayer() {
    34. return this.player;
    35. }
    36.  
    37. public String getName() {
    38. return this.player.getName();
    39. }
    40.  
    41. public File getUserFile() {
    42. return this.PlayerFile;
    43. }
    44.  
    45. public FileConfiguration getUserConfig() {
    46. return this.PlayerConfig;
    47. }
    48.  
    49. public void save()
    50. {
    51. try {
    52. this.PlayerConfig.save(this.PlayerFile);
    53. } catch (IOException e) {
    54. e.printStackTrace();
    55. }
    56. }
    57.  
    58. public void CreatePlayerFile(Player p)
    59. {
    60. if(!PlayerFile.exists()){
    61. try{
    62. PlayerFile.createNewFile();
    63. PlayerConfig = YamlConfiguration.loadConfiguration(PlayerFile);
    64. PlayerConfig.createSection("Levels.Farming");
    65. PlayerConfig.set("Levels.Farming", 0);
    66. PlayerConfig.createSection("Levels.Mining");
    67. PlayerConfig.set("Levels.Mining", 0);
    68. PlayerConfig.createSection("Levels.Smithing");
    69. PlayerConfig.set("Levels.Smithing", 0);
    70. PlayerConfig.createSection("Levels.Hunting");
    71. PlayerConfig.set("Levels.Hunting", 0);
    72. PlayerConfig.createSection("Levels.Cooking");
    73. PlayerConfig.set("Levels.Cooking", 0);
    74. PlayerConfig.createSection("Levels.Combat");
    75. PlayerConfig.set("Levels.Combat", 0);
    76. PlayerConfig.createSection("Levels.Archery");
    77. PlayerConfig.set("Levels.Archery", 0);
    78. PlayerConfig.save(PlayerFile);
    79. }
    80. catch (IOException e){
    81. e.printStackTrace();
    82. }
    83. }
    84. }
    85.  
    86. public int getXP(String level)
    87. {
    88. return this.PlayerConfig.getInt(level);
    89. }
    90.  
    91. public void setXP(String s, int i)
    92. {
    93. getUserConfig().set(s, Integer.valueOf(i));
    94. save();
    95. }
    96.  
    97. public void addExp(String s, int i)
    98. {
    99. int before = getXP(s);
    100. before += i;
    101. setXP(s, before);
    102. }
    103.  
    104. public static API getInstance()
    105. {
    106. return instance;
    107. }
    108. }

    Handlers.class
    Code:java
    1. package Kassestral.Levels;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.BlockBreakEvent;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.plugin.Plugin;
    10.  
    11. public class Handlers implements Listener
    12. {
    13. API API;
    14.  
    15. private final Plugin Levels;
    16. public Handlers(Plugin Levels)
    17. {
    18. this.Levels = Levels;
    19. }
    20.  
    21. @EventHandler
    22. public void onBlockBreak(BlockBreakEvent e)
    23. {
    24. Material Block = e.getBlock().getType();
    25.  
    26. if(Block == Material.STONE)
    27. {
    28. API.addExp("Levels.Mining", 5);
    29. }
    30. }
    31.  
    32. @EventHandler
    33. public void onPlayerJoin(PlayerJoinEvent e)
    34. {
    35. Player p = e.getPlayer();
    36. API.CreatePlayerFile(p);
    37. }
    38. }
     
  2. Offline

    St3venAU

    In your handlers class you declare the variable API but never assign it to anything so it will be null, therefore line 36 API.CreatePlayerFile(p); will cause a null pointer exception since API is null.
     
  3. Offline

    Kassestral

    St3venAU
    Can you show me how to assign it?
     
  4. Offline

    Sabersamus

    First of all, please, please, please, work on your naming.

    API API; is REALLY a bad idea.

    API api;


    Second of all,
    Code:
    public Handler(Plugin levels){
        this.Levels = levels;
        this.api = new API(levels);
    }
    
     
  5. Offline

    Kassestral

    Sabersamus
    It is now coming up with
    Code:
    Caused by: java.lang.NullPointerException
        at Kassestral.Levels.API.CreatePlayerFile(API.java:60) ~[?:?]
        at Kassestral.Levels.Handlers.onPlayerJoin(Handlers.java:37) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    But I thought the thing was claiming p to being e.getplayer() ?

    ?

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

    Sabersamus

    PlayerFile.exists() is throwing the NPE because PlayerFile is null
     
  7. Offline

    Kassestral

    How would I go about fixing that?
     
  8. Offline

    Rocoty

  9. Offline

    Sabersamus


    I just told you how,

    Code:
    public Handlers(Plugin Levels)
    {
        this.levels = levels;
        this.api = new API(levels);
    }
     
  10. Offline

    Kassestral

    I did that but it has that error

    Its alright I fixed it now, thanks for the help thought guys :)

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

    Sabersamus

    please read this, btw.

    Really, it'll help.
     
  12. Offline

    Kassestral

    Yeah I need to get into the habit of using that, because IDK really, I do it the way I was taught xD but I need to learn to name them so everyone else can read it also, il try soon (P.S how do I close a thread?)
     
  13. Offline

    Adriani6

    Kassestral
    If you want everyone to know whats going on in your code try putting in comments, they are really helpful not only for others but for yourself as well. Other thing would be alignment, make sure the indentation is in place, makes code readable. Sensible variable names, would allow people to know what that variable possibly holds. Its also a good idea to keep them lower case so you dont get messed up with other stuff.

    Closing thread? You can ask a member of staff to do it for you by clicking report (I suppose) or just tahg them in. However marking it as solved is enough :)
     
  14. Offline

    Kassestral

Thread Status:
Not open for further replies.

Share This Page