Null Pointer Exception Please help

Discussion in 'Plugin Development' started by boardinggamer, Nov 22, 2012.

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

    boardinggamer

    I am trying to edit a config file to add a player to it but it keeps calling a null pointer exception.

    main class:
    Code:java
    1. package boardinggamer.Ecahmoney;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.configuration.Configuration;
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.plugin.PluginManager;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class ecahmoney extends JavaPlugin implements Listener{
    15.  
    16. MoneyAPI money = MoneyAPI.getInstance();
    17.  
    18. public void onEnable() {
    19.  
    20. PluginManager pm = getServer().getPluginManager();
    21. pm.registerEvents(this, this);
    22.  
    23. createConfigs();
    24. Configuration config = getConfig();
    25. config.addDefault("Money.Name", "Dollor");
    26. config.addDefault("Money.Starting Amount", 25.00);
    27. config.addDefault("Bank.Creation Cost", 10.00);
    28. config.addDefault("Bank.Intrest Rate", 2.50);
    29. File players = new File(getDataFolder(), "players.yml");
    30. FileConfiguration playersConf = YamlConfiguration.loadConfiguration(players);
    31. playersConf.addDefault("Playername.Money", 0.0);
    32. File banks = new File(getDataFolder(), "banks.yml");
    33. FileConfiguration banksConf = YamlConfiguration.loadConfiguration(banks);
    34. banksConf.addDefault("Banksname.Money", 0.0);
    35. banksConf.addDefault("Banksname.Creater", "Playername");
    36. banksConf.addDefault("Banksname.Members", new String[] { "Playername" });
    37. try{
    38. playersConf.options().copyDefaults(true);
    39. playersConf.save(players);
    40. banksConf.options().copyDefaults(true);
    41. banksConf.save(banks);
    42. config.options().copyDefaults(true);
    43. saveConfig();
    44. } catch (Exception e) {}
    45. }
    46.  
    47. public void onDisable() {
    48.  
    49. }
    50.  
    51. public void createConfigs(){
    52. File players = new File(getDataFolder(), "players.yml");
    53. if(players.exists() == false) {
    54. saveResource("players.yml", false);
    55. }
    56. File banks = new File(getDataFolder(), "banks.yml");
    57. if(banks.exists() == false) {
    58. saveResource("banks.yml", false);
    59. }
    60. }
    61.  
    62. @EventHandler
    63. public void joinEvent(PlayerJoinEvent event) {
    64. String name = event.getPlayer().getName();
    65. money.addPlayerToConfig(name);
    66. }
    67.  
    68. }
    69.  


    MoneyAPI cass:
    Code:java
    1. package boardinggamer.Ecahmoney;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.configuration.file.FileConfiguration;
    6. import org.bukkit.configuration.file.YamlConfiguration;
    7.  
    8. public class MoneyAPI {
    9.  
    10. private ecahmoney plugin;
    11. private static MoneyAPI singleton;
    12.  
    13. public MoneyAPI(ecahmoney plugin){
    14. this.plugin = plugin;
    15. singleton = this;
    16. }
    17.  
    18. public static final MoneyAPI getInstance(){
    19. return singleton;
    20. }
    21.  
    22. public boolean playerInMoneyConfig(String name){
    23. boolean in = false;
    24. System.out.println("start false");
    25. File players = new File(this.plugin.getDataFolder(), "players.yml");
    26. System.out.println("got file");
    27. if (players.exists()){
    28. System.out.println("file exists");
    29. FileConfiguration pconf = YamlConfiguration.loadConfiguration(players);
    30.  
    31. System.out.println("got conf");
    32. if (pconf.contains(name + ".Money")){
    33.  
    34. System.out.println("not in file");
    35. in = true;
    36. System.out.println("can be added");
    37. }
    38. }
    39. return in;
    40. }
    41.  
    42. public void addPlayerToConfig(String name){
    43. File players = new File(this.plugin.getDataFolder(), "players.yml");
    44. FileConfiguration pconf = YamlConfiguration.loadConfiguration(players);
    45. double start = this.plugin.getConfig().getDouble("Money.Starting Amount");
    46. if (playerInMoneyConfig(name) == false){
    47. pconf.addDefault(name + ".Money", start);
    48. try{
    49. pconf.options().copyDefaults(true);
    50. pconf.save(players);
    51. } catch (Exception e) {}
    52. }
    53. }
    54.  
    55. }
    56.  


    Error
    Code:bash
    1. c2:05:46 [INFO] creeperzgod[/192.168.1.1:52890] logged in with entity id 8822 at ([world] 255.97760183358474, 66.0, 214.5243049141394)
    2. 12:05:46 [SEVERE] Could not pass event PlayerJoinEvent to Ecahmoney v1.0
    3. org.bukkit.event.EventException
    4. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
    5. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    6. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    7. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    8. at net.minecraft.server.ServerConfigurationManagerAbstract.c(ServerConfigurationManagerAbstract.java:153)
    9. at net.minecraft.server.ServerConfigurationManagerAbstract.a(ServerConfigurationManagerAbstract.java:93)
    10. at net.minecraft.server.NetLoginHandler.d(NetLoginHandler.java:132)
    11. at net.minecraft.server.NetLoginHandler.c(NetLoginHandler.java:45)
    12. at net.minecraft.server.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:44)
    13. at net.minecraft.server.DedicatedServerConnection.b(SourceFile:29)
    14. at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    15. at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    16. at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    17. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    18. at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    19. Caused by: java.lang.NullPointerException
    20. at boardinggamer.Ecahmoney.ecahmoney.joinEvent(ecahmoney.java:65)
    21. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    22. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    23. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    24. at java.lang.reflect.Method.invoke(Method.java:616)
    25. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
    26. ... 14 more
    27.  
     
  2. Offline

    fireblast709

    money is null. You might create a MoneyAPI instance before you call getInstance()
     
  3. Offline

    Jogy34

    Your code makes no sense. A singleton is a class that there can only be one of and this is done by making the constructor private so that you can only access it from within the class. What you should do is either make your constructor private and then do this: MoneyAPI singleton = new MoneyAPI(); in your moneyAPI class or don't make the constructor private and then do this: money = new MoneyAPI(this); in your onEnable() method and then get rid of the MoneyAPI variable in your MoneyAPI class and get rid of the getInstance() method in that class as well. Either one of those should fix your problem but I would suggest the second one so that you don't have to mess with making a variable of your main class since it looks like you would need one in your MoneyAPI class
     
  4. Offline

    boardinggamer

    God damn it I forgot to call the MoneyAPI in the onEnable() >_>

    Ok I will fix that when I get home, thanks.
     
Thread Status:
Not open for further replies.

Share This Page