NullPointerException caused by EconomyResponse

Discussion in 'Plugin Development' started by Donut99, Jun 24, 2014.

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

    Donut99

    Hello Bukkit friends,

    I've started to program a simple GiveAllMoney command using the Vault API. The command can be executed by a Player or ConsoleCommandSender, but everytime when I try to perform the command I get a NullPointerException. The command:

    Code:java
    1. package GiveAll;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import net.milkbowl.vault.economy.EconomyResponse;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.command.ConsoleCommandSender;
    12. import org.bukkit.entity.Player;
    13.  
    14. public class GiveAllMoney implements CommandExecutor{
    15.  
    16. public static Economy econ = null;
    17. int am = 0;
    18.  
    19. @Override
    20. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    21.  
    22. if (sender instanceof ConsoleCommandSender){
    23.  
    24. if (args.length == 0 | args.length > 1){
    25.  
    26. sender.sendMessage("");
    27. return true;
    28.  
    29. }
    30.  
    31. if (args.length == 1){
    32.  
    33. for (Player p : Bukkit.getOnlinePlayers()){
    34.  
    35. int am = Integer.valueOf(args[0]);
    36.  
    37. EconomyResponse r = econ.depositPlayer(p.getName(), am);
    38.  
    39. if (r.transactionSuccess()){
    40.  
    41. sender.sendMessage("");
    42. p.sendMessage("");
    43. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
    44. return true;
    45.  
    46. } else {
    47.  
    48. p.sendMessage("§8§l[§6§lSGS§8§l] §cError:" + r.errorMessage);
    49. return true;
    50.  
    51. }
    52. }
    53. }
    54. }
    55.  
    56. if (sender instanceof Player){
    57.  
    58. if (sender.hasPermission("SkyGamesSystem.GiveAllMoney")){
    59.  
    60. if (args.length == 0 | args.length > 1){
    61.  
    62. sender.sendMessage("");
    63. return true;
    64.  
    65. }
    66.  
    67. if (args.length == 1){
    68.  
    69. int am = Integer.valueOf(args[0]);
    70.  
    71. try{
    72.  
    73. } catch (NumberFormatException e){
    74.  
    75. sender.sendMessage("");
    76. return true;
    77.  
    78. }
    79.  
    80. for (Player p : Bukkit.getOnlinePlayers()){
    81.  
    82. EconomyResponse r = econ.depositPlayer(p.getName(), am);
    83.  
    84. if (r.transactionSuccess()){
    85.  
    86. sender.sendMessage("");
    87. p.sendMessage("");
    88. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
    89. return true;
    90.  
    91. } else {
    92.  
    93. sender.sendMessage("§8§l[§6§lSGS§8§l] §cError:" + r.errorMessage);
    94. return true;
    95.  
    96. }
    97. }
    98. }
    99. } else {
    100.  
    101. sender.sendMessage("§8§l[§6§lSGS§8§l] §cYou do not have permission to perform this command!");
    102. return true;
    103.  
    104. }
    105. }
    106.  
    107. return true;
    108.  
    109. }
    110. }
    111.  


    The NullPointerException:

    Code:java
    1. [00:00:00] [Server thread/WARN]: Unexpected exception while parsing console command "gam 1"
    2. org.bukkit.command.CommandException: Unhandled exception executing command 'gam' in plugin SkyGamesSystem v3.0
    3. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    4. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    5. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    6. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:670) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    7. at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    8. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    9. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    10. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    11. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    12. Caused by: java.lang.NullPointerException
    13. at GiveAll.GiveAllMoney.onCommand(GiveAllMoney.java:37) ~[?:?]
    14. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    15.  


    37: EconomyResponse r = econ.depositPlayer(p.getName(), am);

    I don't really know what to do to get it working. I hope anybody can help me. Everey little bit of help is appreciated!
     
  2. Offline

    CorrieKay

    econ is null, as far as I can tell. Then again, its public static, so you may be initializing it elsewhere, but thats the most glaring problem i can see right now.
     
  3. Offline

    Donut99

    Sorry, can't get it work. But thank you for helping me.
    Anybody else knows what to do?
     
  4. Offline

    1Rogue

    Where do you initialize the econ variable?
    Why is it public?
    Why is it static?
     
  5. Offline

    Donut99

    The econ variable is initialized in main class:
    Code:java
    1. private boolean setupEconomy() {
    2.  
    3. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    4.  
    5. return false;
    6.  
    7. }
    8.  
    9. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    10.  
    11. if (rsp == null) {
    12.  
    13. return false;
    14.  
    15. }
    16.  
    17. econ = rsp.getProvider();
    18. return econ != null;
    19.  
    20. }
     
  6. Donut99
    Here's an interesting question; do you even know what's going on in your code?
     
  7. Offline

    Donut99

    It's not my code, its the normal way to setup Economy using the Vault API. If anything is wrong you can tell me...
     
  8. All this
    Code:java
    1. int am = 0;
    2.  
    3. @Override
    4. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
    5.  
    6. if (sender instanceof ConsoleCommandSender) {
    7.  
    8. if (args.length == 0 | args.length > 1) {
    9.  
    10. sender.sendMessage("");
    11. return true;
    12.  
    13. }
    14.  
    15. if (args.length == 1) {
    16.  
    17. for (Player p: Bukkit.getOnlinePlayers()) {
    18.  
    19. int am = Integer.valueOf(args[0]);
    20.  
    21. EconomyResponse r = econ.depositPlayer(p.getName(), am);
    22.  
    23. if (r.transactionSuccess()) {
    24.  
    25. sender.sendMessage("");
    26. p.sendMessage("");
    27. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
    28. return true;
    29.  
    30. } else {
    31.  
    32. p.sendMessage("§8§l[§6§lSGS§8§l] §cError:" + r.errorMessage);
    33. return true;
    34.  
    35. }
    36. }
    37. }
    38. }
    39.  
    40. if (sender instanceof Player) {
    41.  
    42. if (sender.hasPermission("SkyGamesSystem.GiveAllMoney")) {
    43.  
    44. if (args.length == 0 | args.length > 1) {
    45.  
    46. sender.sendMessage("");
    47. return true;
    48.  
    49. }
    50.  
    51. if (args.length == 1) {
    52.  
    53. int am = Integer.valueOf(args[0]);
    54.  
    55. try {
    56.  
    57. } catch (NumberFormatException e) {
    58.  
    59. sender.sendMessage("");
    60. return true;
    61.  
    62. }
    63.  
    64. for (Player p: Bukkit.getOnlinePlayers()) {
    65.  
    66. EconomyResponse r = econ.depositPlayer(p.getName(), am);
    67.  
    68. if (r.transactionSuccess()) {
    69.  
    70. sender.sendMessage("");
    71. p.sendMessage("");
    72. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
    73. return true;
    74.  
    75. } else {
    76.  
    77. sender.sendMessage("§8§l[§6§lSGS§8§l] §cError:" + r.errorMessage);
    78. return true;
    79.  
    80. }
    81. }
    82. }
    83. } else {
    84.  
    85. sender.sendMessage("§8§l[§6§lSGS§8§l] §cYou do not have permission to perform this command!");
    86. return true;
    87.  
    88. }
    89. }
    90.  
    91. return true;
    92.  
    93. }

    is "the normal way to setup Economy using the Vault API"?

    You never even check what command was executed, most of your messages are empty (why?!), and what in the world is this?
    Code:java
    1. try {
    2.  
    3. } catch (NumberFormatException e) {
    4. sender.sendMessage("");
    5. return true;
    6. }
     
  9. Offline

    1Rogue

    if you set the variable "econ" in a different class, it won't be set for all classes with an "econ" variable.
    If setupEconomy() fails, you're still running code as if it hadn't either.
     
  10. Offline

    Donut99

    No ;) The command is registered in main class:

    Code:java
    1. getCommand("gam").setExecutor(new GiveAllMoney());


    The messages should be edited later because I haven't already thought about it. And the try exception shouldn't be there, but it's not the problem because if it's a ConsoleCommandSender it also doesn't work anyways.

    Sure. I set the econ variable in main class and also in GiveAllMoney class.
    And for If setupEconomy() fails I have this in onEnable:

    Code:java
    1. if (!setupEconomy()){
    2.  
    3. getLogger().severe(String.format("[%s] - Vault couldn't be found!", getDescription().getName()));
    4. return;
    5.  
    6. }


    Please. Anybody? I need it. I can't get it work.

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

    xTigerRebornx

    Donut99 Read 1Rogue 's post, your plugin is still enabled even if there is no economy present, and he pointed out the problem.
     
  12. Offline

    Donut99

    So I got this:
    Code:java
    1. if (!setupEconomy()){
    2.  
    3. getLogger().severe(String.format("[%s] - Vault couldn't be found plugins has been disabled", getDescription().getName()));
    4. getServer().getPluginManager().disablePlugin(this);
    5. return;
    6.  
    7. }


    But still get the same NullPointerException.
     
  13. Offline

    CorrieKay

    because youre initializing the econ variable in your main class, and only declaring it in your command executor. The command is using the local variable, and not the initialized variable. Either call a method returning the econ variable from your main class, or skip that and initialize it in the command executor.
     
  14. Offline

    fireblast709

    Donut99 you clearly set econ to null on line 16, while you attempt to use it (while still null, this is the only assignment in the whole command class). Then, when you try to use it on line 37, it will look for all 'econ' inside the scope, and pick the one that is closest to the scope you are currently in. This happens to be the econ on line 16, while still null. Then you try to call a method on that same null, which invokes a nice NullPointerException. In order to access that econ, you need to initialize it (perhaps removing that static keyword and using a constructor would work ;)).
     
  15. Offline

    Donut99

    Sorry. Don't know how to use methods for anything. Hardly ever I have done anything with constructors.
     
  16. Offline

    1Rogue

  17. Offline

    Donut99

    Code:java
    1. public GiveAllMoney(Economy econ){
    2.  
    3. Economy econ = econ;
    4.  
    5. }


    Is that the right direction?
     
  18. Offline

    AmShaegar

    Nearly. I'd replace this:
    Code:java
    1. public static Economy econ = null;

    with this:
    Code:java
    1. private Economy econ;
    2.  
    3. public GiveAllMoney(Economy econ) {
    4. this.econ = econ;
    5. }
     
  19. Offline

    Donut99

    Doesn't work anyways, main class:
    Code:java
    1. public static Economy econ = null;

    GiveAllMoney class:
    Code:java
    1. private Economy econ;
    2.  
    3. public GiveAllMoney(Economy econ){
    4.  
    5. this.econ = econ;
    6.  
    7. }

    And now I get this NullPointerException while running the command:
    Code:java
    1. [00:00:00] [Server thread/WARN]: Unexpected exception while parsing console command "gam 1"
    2. org.bukkit.command.CommandException: Unhandled exception executing command 'gam' in plugin SkyGamesSystem v3.0
    3. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    4. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    5. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    6. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:670) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    7. at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    8. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    9. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    10. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    11. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    12. Caused by: java.lang.NullPointerException
    13. at GiveAll.GiveAllMoney.onCommand(GiveAllMoney.java:44) ~[?:?]
    14. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    15. ... 8 more
    16.  


    Anything wrong?

    Bump. Nobody knows why?

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

    Gater12

  21. Offline

    Donut99

    The GiveAllItems class:
    Code:java
    1. package GiveAll;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import net.milkbowl.vault.economy.EconomyResponse;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.command.ConsoleCommandSender;
    12. import org.bukkit.entity.Player;
    13.  
    14. public class GiveAllMoney implements CommandExecutor{
    15.  
    16. int am = 0;
    17.  
    18. private Economy econ;
    19.  
    20. public GiveAllMoney(Economy econ){
    21.  
    22. this.econ = econ;
    23.  
    24. }
    25.  
    26. @Override
    27. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    28.  
    29. if (sender instanceof ConsoleCommandSender){
    30.  
    31. if (args.length == 0 | args.length > 1){
    32.  
    33. sender.sendMessage("§8§l[§6§lSGS§8§l] §cUse: §b/GiveAllMoney <Amount>");
    34. return true;
    35.  
    36. }
    37.  
    38. if (args.length == 1){
    39.  
    40. for (Player p : Bukkit.getOnlinePlayers()){
    41.  
    42. int am = Integer.valueOf(args[0]);
    43.  
    44. EconomyResponse r = econ.depositPlayer(p.getName(), am);
    45.  
    46. if (r.transactionSuccess()){
    47.  
    48. sender.sendMessage("");
    49. p.sendMessage("");
    50. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
    51. return true;
    52.  
    53. } else {
    54.  
    55. p.sendMessage("§8§l[§6§lSGS§8§l] §cError:" + r.errorMessage);
    56. return true;
    57.  
    58. }
    59. }
    60. }
    61. }
    62.  
    63. if (sender instanceof Player){
    64.  
    65. if (sender.hasPermission("SkyGamesSystem.GiveAllMoney")){
    66.  
    67. if (args.length == 0 | args.length > 1){
    68.  
    69. sender.sendMessage("§8§l[§6§lSGS§8§l] §cUse: §b/GiveAllMoney <Amountl>");
    70. return true;
    71.  
    72. }
    73.  
    74. if (args.length == 1){
    75.  
    76. int am = Integer.valueOf(args[0]);
    77.  
    78. try{
    79.  
    80. } catch (NumberFormatException e){
    81.  
    82. sender.sendMessage("");
    83. return true;
    84.  
    85. }
    86.  
    87. for (Player p : Bukkit.getOnlinePlayers()){
    88.  
    89. EconomyResponse r = econ.depositPlayer(p.getName(), am);
    90.  
    91. if (r.transactionSuccess()){
    92.  
    93. sender.sendMessage("");
    94. p.sendMessage("");
    95. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
    96. return true;
    97.  
    98. } else {
    99.  
    100. sender.sendMessage("§8§l[§6§lSGS§8§l] §cError" + r.errorMessage);
    101. return true;
    102.  
    103. }
    104. }
    105. }
    106. } else {
    107.  
    108. sender.sendMessage("");
    109. return true;
    110.  
    111. }
    112. }
    113.  
    114. return true;
    115.  
    116. }
    117. }


    Main class:
    Code:java
    1. public static Economy econ = null;


    Anything wrong? Get the NullPointerException above.
     
  22. Offline

    AmShaegar

    Everybody knows why and we already told you: Your econ is null. Even if you pass it, which was a great step into the right direction, it still contains null. You are passing nothing else than null.
    Setup Economy in your onEnable() and make sure it gets assigned to econ.
     
Thread Status:
Not open for further replies.

Share This Page