Solved Config is acting null? Why though? (Code Supplied)

Discussion in 'Plugin Development' started by MayoDwarf, Dec 24, 2013.

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

    MayoDwarf

    Code:java
    1. package Achievements;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.RegisteredServiceProvider;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. /**
    12. * Created with IntelliJ IDEA.
    13. * User: MayoDwarf
    14. * Date: 12/23/13
    15. * Time: 8:26 PM
    16. * To change this template use File | Settings | File Templates.
    17. */
    18. public class Main extends JavaPlugin {
    19. public String title = ChatColor.GRAY+"["+ChatColor.AQUA+"Prison Achievements"+ChatColor.GRAY+"]"+ChatColor.RESET;
    20. public FileConfiguration config;
    21. public static Economy econ = null;
    22. public void onEnable() {
    23. this.saveDefaultConfig();
    24. this.getConfig().options().copyDefaults(true);
    25. this.saveConfig();
    26. config.addDefault("Players.MayoDwarf.Mined", 1);
    27. config.addDefault("Players.MayoDwarf.Placed", 1);
    28. config.addDefault("Players.MayoDwarf.Killed", 1);
    29. config = getConfig();
    30. this.getCommand("achievement").setExecutor(new CommandReg());
    31. this.getServer().getPluginManager().registerEvents(new EvtListener(), this);
    32. setupEconomy();
    33. }
    34. public void onDisable() {
    35. }
    36. public void onJoin(Player p) {
    37. if(config == null) {
    38. System.out.print("Fuck, the config is null and DSH is right.");
    39. } else {
    40. System.out.print("I am right. Fuck you DSH. The config is not null, but however decides not to work right.");
    41. }
    42. if(!config.contains("Players."+p.getName())) {
    43. config.addDefault("Players."+p.getName()+".Mined", 1);
    44. config.addDefault("Players."+p.getName()+".Placed", 1);
    45. config.addDefault("Players."+p.getName()+".Killed", 1);
    46. //Coming Soon:
    47. config.addDefault("Players."+p.getName()+".smelted", 1);
    48. }
    49. }
    50. public void mine(Player p) {
    51. config.set("Players."+p.getName()+".Mined", config.getInt("Players."+p.getName()+".Mined")+1);
    52. this.saveConfig();
    53. if(config.getInt("Players."+p.getName()+".Mined") == 500) {
    54. econ.depositPlayer(p.getName(), 500);
    55. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$500"+ChatColor.YELLOW+" for mining 500 blocks!");
    56. } else
    57. if(config.getInt("Players."+p.getName()+".Mined") == 1000) {
    58. econ.depositPlayer(p.getName(), 5000);
    59. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$5000"+ChatColor.YELLOW+" for mining 1000 blocks!");
    60. } else
    61. if(config.getInt("Players."+p.getName()+".Mined") == 5000) {
    62. econ.depositPlayer(p.getName(), 10000);
    63. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$10000"+ChatColor.YELLOW+" for mining 5000 blocks!");
    64. }
    65. }
    66. public void place(Player p) {
    67. config.set("Players."+p.getName()+".Placed", config.getInt("Players."+p.getName()+".Placed")+1);
    68. this.saveConfig();
    69. if(config.getInt("Players."+p.getName()+".Placed") == 500) {
    70. econ.depositPlayer(p.getName(), 500);
    71. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$500"+ChatColor.YELLOW+" for placing 500 blocks!");
    72. } else
    73. if(config.getInt("Players."+p.getName()+".Placed") == 1000) {
    74. econ.depositPlayer(p.getName(), 5000);
    75. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$5000"+ChatColor.YELLOW+" for placing 1000 blocks!");
    76. } else
    77. if(config.getInt("Players."+p.getName()+".Placed") == 5000) {
    78. econ.depositPlayer(p.getName(), 10000);
    79. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$10000"+ChatColor.YELLOW+" for placing 5000 blocks!");
    80. }
    81. }
    82. public void smelt(Player p) {
    83. }
    84. public void kill(Player p) {
    85. config.set("Players."+p.getName()+".Killed", config.getInt("Players."+p.getName()+".Killed")+1);
    86. this.saveConfig();
    87. if(config.getInt("Players."+p.getName()+".Killed") == 100) {
    88. econ.depositPlayer(p.getName(), 500);
    89. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$500"+ChatColor.YELLOW+" for slaying 100 players!");
    90. } else
    91. if(config.getInt("Players."+p.getName()+".Killed") == 500) {
    92. econ.depositPlayer(p.getName(), 5000);
    93. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$5000"+ChatColor.YELLOW+" for slaying 500 players!");
    94. } else
    95. if(config.getInt("Players."+p.getName()+".Killed") == 1000) {
    96. econ.depositPlayer(p.getName(), 10000);
    97. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$10000"+ChatColor.YELLOW+" for slaying 1000 players!");
    98. Bukkit.broadcastMessage(title+""+" "+ChatColor.RED+""+p.getName()+ChatColor.GOLD+" has slayed 1000 players on this server! Give him a congrats! Beware!");
    99. }
    100. }
    101. private boolean setupEconomy()
    102. {
    103. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    104. if (economyProvider != null) {
    105. econ = economyProvider.getProvider();
    106. }
    107.  
    108. return (econ != null);
    109. }
    110. }
    111.  

    I have made the config in the project also and exported it correctly too.
    Why is it not working. When the stacktrace comes up in the console, it says there is a NPE on line 26. The config is null, however I do not know why. :( Please help, thanks.
     
  2. Offline

    FinalFred

    "config" is just a variable. You never set it's value to anything prior to line 26, so on line 26 it's still the default value (null).
     
  3. Offline

    MayoDwarf

    What should I set it to?

    FinalFred

    Would putting config = getConfig(); above it work?
     
  4. Offline

    Rocoty

    The answer to that should be fairly simple to guess. Either way, you wouldn't have had to guess were it that you only properly knew Java. Variables and references are basic concepts of Java and should be understood before you start coding plugins.
     
    ZeusAllMighty11 likes this.
  5. Offline

    MayoDwarf

    No I know Java, but I believe I had it above it before and it was still coming out null, so I am just making sure. No need to come here and say "You should learn Java" when I already know Java, thank you very much.
     
  6. Offline

    Rocoty

    Sorry, I was just making a statement based on evidence. Evidently, judging by the way you don't seem to understand how variables and references work, I was assuming you didn't. Of course I can see that you know Java to some extent and I never said you don't. But when you miss on such a fundamental part of basic programming it's fair of me to doubt, surely?
     
  7. Offline

    MayoDwarf

    Yah I understand. Sometimes we all make typos though :p. I mean merely all of my errors are always me just messing up a variable placing or something xD. It's never anything else :/. It gets frustrating if you understand haha.

    So I changed up the code. Put the config = getConfig(); in front. It is still coming up as null as you can tell from the error:
    Code:java
    1. [12:19:51 INFO]: mrbill2011[/192.168.1.120:62198] logged in with entity id 14096
    2. at ([world] -187.45989704957398, 59.0, 265.8261461108659)
    3. [12:19:51 INFO]: Fuck, the config is null and DSH is right.
    4. [12:19:51 ERROR]: Could not pass event PlayerJoinEvent to Achievements v1.0
    5. org.bukkit.event.EventException
    6. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    7. va:427) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    8. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    9. a:62) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    10. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    11. ava:477) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    12. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    13. ava:462) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    14. at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [craft
    15. bukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    16. at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [craft
    17. bukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    18. at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [
    19. craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    20. at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [
    21. craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    22. at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149
    23. ) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    24. at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    25. tbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    26. at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    27. 45) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    28. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    29. 50) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    30. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    31. 35) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    32. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    33. :447) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    34. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    35. 17) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    36. Caused by: java.lang.NullPointerException
    37. at Achievements.Main.onJoin(Main.java:41) ~[?:?]
    38. at Achievements.EvtListener.join(EvtListener.java:33) ~[?:?]
    39. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    40. _45]
    41. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    42. _45]
    43. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    44. .7.0_45]
    45. at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
    46. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    47. va:425) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-6-g6c5896d-b2927jnks]
    48. ... 14 more

    Code:java
    1. package Achievements;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.RegisteredServiceProvider;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. /**
    12. * Created with IntelliJ IDEA.
    13. * User: MayoDwarf
    14. * Date: 12/23/13
    15. * Time: 8:26 PM
    16. * To change this template use File | Settings | File Templates.
    17. */
    18. public class Main extends JavaPlugin {
    19. public String title = ChatColor.GRAY+"["+ChatColor.AQUA+"Prison Achievements"+ChatColor.GRAY+"]"+ChatColor.RESET;
    20. public FileConfiguration config;
    21. public static Economy econ = null;
    22. public void onEnable() {
    23. config = getConfig();
    24. this.saveConfig();
    25. config.addDefault("Players.MayoDwarf.Mined", 1);
    26. config.addDefault("Players.MayoDwarf.Placed", 1);
    27. config.addDefault("Players.MayoDwarf.Killed", 1);
    28. this.saveDefaultConfig();
    29. this.getCommand("achievement").setExecutor(new CommandReg());
    30. this.getServer().getPluginManager().registerEvents(new EvtListener(), this);
    31. setupEconomy();
    32. }
    33. public void onDisable() {
    34. }
    35. public void onJoin(Player p) {
    36. if(config == null) {
    37. System.out.print("Fuck, the config is null and DSH is right.");
    38. } else {
    39. System.out.print("I am right. Fuck you DSH. The config is not null, but however decides not to work right.");
    40. }
    41. if(!config.contains("Players."+p.getName())) {
    42. config.addDefault("Players."+p.getName()+".Mined", 1);
    43. config.addDefault("Players."+p.getName()+".Placed", 1);
    44. config.addDefault("Players."+p.getName()+".Killed", 1);
    45. //Coming Soon:
    46. config.addDefault("Players."+p.getName()+".smelted", 1);
    47. }
    48. }
    49. public void mine(Player p) {
    50. config.set("Players."+p.getName()+".Mined", config.getInt("Players."+p.getName()+".Mined")+1);
    51. this.saveConfig();
    52. if(config.getInt("Players."+p.getName()+".Mined") == 500) {
    53. econ.depositPlayer(p.getName(), 500);
    54. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$500"+ChatColor.YELLOW+" for mining 500 blocks!");
    55. } else
    56. if(config.getInt("Players."+p.getName()+".Mined") == 1000) {
    57. econ.depositPlayer(p.getName(), 5000);
    58. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$5000"+ChatColor.YELLOW+" for mining 1000 blocks!");
    59. } else
    60. if(config.getInt("Players."+p.getName()+".Mined") == 5000) {
    61. econ.depositPlayer(p.getName(), 10000);
    62. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$10000"+ChatColor.YELLOW+" for mining 5000 blocks!");
    63. }
    64. }
    65. public void place(Player p) {
    66. config.set("Players."+p.getName()+".Placed", config.getInt("Players."+p.getName()+".Placed")+1);
    67. this.saveConfig();
    68. if(config.getInt("Players."+p.getName()+".Placed") == 500) {
    69. econ.depositPlayer(p.getName(), 500);
    70. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$500"+ChatColor.YELLOW+" for placing 500 blocks!");
    71. } else
    72. if(config.getInt("Players."+p.getName()+".Placed") == 1000) {
    73. econ.depositPlayer(p.getName(), 5000);
    74. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$5000"+ChatColor.YELLOW+" for placing 1000 blocks!");
    75. } else
    76. if(config.getInt("Players."+p.getName()+".Placed") == 5000) {
    77. econ.depositPlayer(p.getName(), 10000);
    78. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$10000"+ChatColor.YELLOW+" for placing 5000 blocks!");
    79. }
    80. }
    81. public void smelt(Player p) {
    82. }
    83. public void kill(Player p) {
    84. config.set("Players."+p.getName()+".Killed", config.getInt("Players."+p.getName()+".Killed")+1);
    85. this.saveConfig();
    86. if(config.getInt("Players."+p.getName()+".Killed") == 100) {
    87. econ.depositPlayer(p.getName(), 500);
    88. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$500"+ChatColor.YELLOW+" for slaying 100 players!");
    89. } else
    90. if(config.getInt("Players."+p.getName()+".Killed") == 500) {
    91. econ.depositPlayer(p.getName(), 5000);
    92. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$5000"+ChatColor.YELLOW+" for slaying 500 players!");
    93. } else
    94. if(config.getInt("Players."+p.getName()+".Killed") == 1000) {
    95. econ.depositPlayer(p.getName(), 10000);
    96. p.sendMessage(title+""+" "+ChatColor.YELLOW+"You have recieved "+ChatColor.RED+"$10000"+ChatColor.YELLOW+" for slaying 1000 players!");
    97. Bukkit.broadcastMessage(title+""+" "+ChatColor.RED+""+p.getName()+ChatColor.GOLD+" has slayed 1000 players on this server! Give him a congrats! Beware!");
    98. }
    99. }
    100. private boolean setupEconomy()
    101. {
    102. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    103. if (economyProvider != null) {
    104. econ = economyProvider.getProvider();
    105. }
    106.  
    107. return (econ != null);
    108. }
    109. }
    110.  


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

    Rocoty

    Are you setting the config back to null (or to something else) somewhere in another class? Can you post the EvtListener class?
     
  9. Offline

    MayoDwarf

    Rocoty Nope I'm not. Sure, one second please.

    EvtListener ==
    Code:java
    1. package Achievements;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.block.BlockBreakEvent;
    6. import org.bukkit.event.block.BlockPlaceEvent;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. /**
    12. * Created with IntelliJ IDEA.
    13. * User: MayoDwarf
    14. * Date: 12/23/13
    15. * Time: 8:41 PM
    16. * To change this template use File | Settings | File Templates.
    17. */
    18. public class EvtListener extends Main implements Listener {
    19. @EventHandler
    20. public void onBlock(BlockBreakEvent evt) {
    21. mine(evt.getPlayer());
    22. }
    23. @EventHandler
    24. public void onBuild(BlockPlaceEvent evt) {
    25. place(evt.getPlayer());
    26. }
    27. @EventHandler
    28. public void kill(PlayerDeathEvent evt) {
    29. kill(evt.getEntity().getKiller());
    30. }
    31. @EventHandler
    32. public void join(PlayerJoinEvent evt) {
    33. onJoin(evt.getPlayer());
    34. }
    35. }

    CommandReg ==
    Code:java
    1. package Achievements;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.command.Command;
    7.  
    8. /**
    9. * Created with IntelliJ IDEA.
    10. * User: MayoDwarf
    11. * Date: 12/23/13
    12. * Time: 8:40 PM
    13. * To change this template use File | Settings | File Templates.
    14. */
    15. public class CommandReg extends Main implements CommandExecutor {
    16. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args){
    17. if(cmd.getName().equalsIgnoreCase("achievement")) {
    18. if(args.length == 0) {
    19. sender.sendMessage(title+""+" "+ ChatColor.DARK_RED+"Type /achievement help for more information ;)");
    20. }
    21. if(args.length == 1) {
    22. if(args[0].equalsIgnoreCase("help")) {
    23. sender.sendMessage(ChatColor.RED+"---- HELP -----");
    24. sender.sendMessage(ChatColor.AQUA+"/achievement info <mining, building, killing> - Find out information for the achievements aquirable in this plugin.");
    25. sender.sendMessage(ChatColor.RED+"---------------");
    26. }
    27. if(args[0].equalsIgnoreCase("info")) {
    28. sender.sendMessage(title+""+" "+ChatColor.DARK_RED+"Usage: /achievement info <mining, building, killing>");
    29. }
    30. }
    31. if(args.length == 2) {
    32. if(args[0].equalsIgnoreCase("info")) {
    33. if(args[1].equalsIgnoreCase("mining")) {
    34. sender.sendMessage(ChatColor.AQUA+"---- MINING ----");
    35. sender.sendMessage(ChatColor.GREEN+"500 Blocks Mined == $500");
    36. sender.sendMessage(ChatColor.GREEN+"1000 Blocks Mined == $5000");
    37. sender.sendMessage(ChatColor.GREEN+"5000 Blocks Mined == $10000");
    38. sender.sendMessage(ChatColor.RED+"Your Mined Blocks: "+ChatColor.GOLD+""+config.getInt("Players."+sender.getName()+".Mined"));
    39. sender.sendMessage(ChatColor.AQUA+"----------------");
    40. }
    41. if(args[1].equalsIgnoreCase("killing")) {
    42. sender.sendMessage(ChatColor.AQUA+"---- KILLING ----");
    43. sender.sendMessage(ChatColor.GREEN+"100 Kills == $500");
    44. sender.sendMessage(ChatColor.GREEN+"500 Kills == $5000");
    45. sender.sendMessage(ChatColor.GREEN+"1000 Kills == $10000");
    46. sender.sendMessage(ChatColor.RED+"Your Killings: "+ChatColor.GOLD+""+config.getInt("Players."+sender.getName()+".Killed"));
    47. sender.sendMessage(ChatColor.AQUA+"-----------------");
    48. }
    49. if(args[1].equalsIgnoreCase("building")) {
    50. sender.sendMessage(ChatColor.AQUA+"---- BUILDING ----");
    51. sender.sendMessage(ChatColor.GREEN+"500 Placed Blocks == $500");
    52. sender.sendMessage(ChatColor.GREEN+"1000 Placed Blocks == $5000");
    53. sender.sendMessage(ChatColor.GREEN+"5000 Placed Blocks == $10000");
    54. sender.sendMessage(ChatColor.RED+"Your Placings: "+ChatColor.GOLD+""+config.getInt("Players."+sender.getName()+".Placed"));
    55. sender.sendMessage(ChatColor.AQUA+"------------------");
    56. }
    57. }
    58. }
    59. }
    60. return true;
    61. }
    62. }
    63.  

    EDIT: Rocoty

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

    Rocoty

    See, there is your problem. You are not calling the method you think you are calling here
    Code:java
    1. @EventHandler
    2. public void join(PlayerJoinEvent evt) {
    3. onJoin(evt.getPlayer());
    4. }

    You are actually calling a method inherited from the main class, and not the method from the main class itself. The config is also inherited, but the inherited version is never initialised in the EvtListener class, thus it is null.

    You should instead invoke the method in the main instance. You can get a reference to the main instance by passing it through an explicitly declared constructor and saving it in a field when instantiating the EvtListener class.

    It is hard to explain, but read up on Java OOP and inheritance and constructors. The following links should be great learning sources
    http://docs.oracle.com/javase/tutorial/java/concepts/
    http://docs.oracle.com/javase/tutorial/java/javaOO/index.html

    EDIT: Just a tip. You should never need to extend JavaPlugin in any other class than your main class. You should also never need to extend your main class anywhere.
     
  11. Offline

    MayoDwarf

    Rocoty, it is going through though. See how in the error it was actually going through the code. The problem is with the config being null. Rocoty
     
  12. Offline

    Rocoty

    Yes, it is going through. But the entire method body is inherited and implicitly copied from the main class to the EvtListener class. I'm sorry, but you have to rethink what you are doing. PS: Read my edit above. ^
     
  13. Offline

    MayoDwarf

    How come this worked in my other plugins though? :/
     
  14. Offline

    ZeusAllMighty11

    Code:
    File config = new File(getDataFolder() + "/config.yml");
    if(!config.exists())
        saveDefaultConfig();
     
    getConfig().set("this.is.a.path", "hello world");
    saveConfig();
    
    You should not be saving the default config every time the server starts up.
     
  15. Offline

    Rocoty

    I don't know what you did in your other plugins, but it cannot have been exactly the same. Maybe you weren't using the config in the other classes.

    ZeusAllMighty11 the saveDefaultConfig() method checks that automatically before trying to save, so you can safely call it in onEnable without any extra checks.
     
  16. Offline

    MayoDwarf

    Also, the folder is not being created for the plugin. Which should be done when the config is called to create. Any ideas why it is not creating the config? I found out that is the reason as to why it is coming up null (It's not being created for some odd reason). Thanks - Jared
     
  17. Offline

    Rocoty

    When you are referencing the config you are reading data which has previously been read from the config file and stored in memory. In other words you are not reading directly from the config. So whatever is causing the config not to save, it has nothing to do with the variable referencing null.

    I want you to try something, though. Please do the following and tell me what happened.
    First, define a method in the main class like so:
    Code:java
    1. public void checkConfig() {
    2. System.out.println(config == null ? "Config is null" : "Config is not null");
    3. }

    Then invoke it in onEnable with a println indicating where we are at:
    Code:java
    1. System.out.println("onEnable:");
    2. checkConfig();

    Then change the join method in EvtListener to the following:
    Code:java
    1. @EventHandler
    2. public void join(PlayerJoinEvent evt) {
    3. System.out.println("join");
    4. checkConfig();
    5. }


    Please do that and tell me the output. And if just reading the output yourself clears stuff up for you, that's good.
     
  18. Offline

    MayoDwarf

    Rocoty
    Code:java
    1. O]: [Achievements] Enabling Achievements v1.0
    2. [13:20:52 INFO]: Config is not null
    3. [13:20:52 INFO]: Server permissions file permissions.yml is empty, ignoring it
    4. [13:20:52 INFO]: CONSOLE: Reload complete.
    5. [13:21:04 INFO]: UUID of player MayoDwarf is dee7ec28ceb643c6b700238c8cf8d9b1
    6. [13:21:04 INFO]: MayoDwarf[/127.0.0.1:50632] logged in with entity id 13780 at ([world] 153.60516779256577, 72.0, 661.6171993731356)
    7. [13:21:04 INFO]: join
    8. [13:21:04 INFO]: Config is null
     
  19. Offline

    Rocoty

    Do you see now? You are using two different config variables, referencing two completely different things. The one in your main class actually references the config. The one in your EvtHandler references a null pointer. Because the plugin loader was never told to initialise the config for you in the EvtHandler, only in Main.

    So please follow my previous advice:
     
  20. Offline

    MayoDwarf

    Those links don't really help too much.
     
  21. Offline

    Rocoty

    You're right. They don't help you solve your particular problem directly. But they help you understand, which in turn helps you solve your problem.
     
  22. Offline

    MayoDwarf

    Rocoty I fixed it! Thanks! :p
     
  23. Offline

    Rocoty

    MayoDwarf No problem, man. But tell me. Did the links help at all?
     
  24. Offline

    MayoDwarf

    No, not much at all. I looked up Main Instance bukkit and found someone's code and read it. I am more intrigued with code than with words :p Rocoty You gave me what to search up tho haha so thx ;)
     
  25. Offline

    Blah1

    Store your players in different files instead of just the config.
     
  26. Offline

    Garris0n


    Code:
      public void saveDefaultConfig() {
        if (!this.configFile.exists())
          saveResource("config.yml", false);
      }
    
    The method does the same thing, no need to do the check twice...
     
Thread Status:
Not open for further replies.

Share This Page