Solved Help with Error

Discussion in 'Plugin Development' started by Jaaakee224, Apr 14, 2014.

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

    Jaaakee224

    I am creating a plugin that will show your balance once you click on a custom created Villager.
    I was thrown a error which I can't resolve, the entire plugin looks fine and I don't receive any errors in Eclipse at all.

    Here is the error I receive
    Code:java
    1. 7:57:11 PM [javastacktrace] java.lang.NullPointerException
    2. 7:57:11 PM [javastacktrace] at me.Jaaakee224.VillagerClick.VillagerClick.setupVault(VillagerClick.java:28) ~[?:?]
    3. 7:57:11 PM [javastacktrace] at me.Jaaakee224.VillagerClick.VillagerClick.onEnable(VillagerClick.java:21) ~[?:?]
    4. 7:57:11 PM [javastacktrace] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    5. 7:57:11 PM [javastacktrace] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    6. 7:57:11 PM [javastacktrace] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:389) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    7. 7:57:11 PM [javastacktrace] at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:439) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    8. 7:57:11 PM [javastacktrace] at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:375) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    9. 7:57:11 PM [javastacktrace] at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:342) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    10. 7:57:11 PM [javastacktrace] at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:319) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    11. 7:57:11 PM [javastacktrace] at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:275) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    12. 7:57:11 PM [javastacktrace] at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:175) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]
    13. 7:57:11 PM [javastacktrace] at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-42-g0551358-b3015jnks]


    VillagerClick (Main Class)
    Code:java
    1. package me.Jaaakee224.VillagerClick;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.ConsoleCommandSender;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.plugin.Plugin;
    9. import org.bukkit.plugin.PluginManager;
    10. import org.bukkit.plugin.RegisteredServiceProvider;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class VillagerClick extends JavaPlugin {
    14.  
    15. public static Economy econ = null;
    16. public ConsoleCommandSender console;
    17.  
    18.  
    19. @Override
    20. public void onEnable() {
    21. Bukkit.getServer().getPluginManager().registerEvents((Listener) new Events(), this);
    22.  
    23. setupVault(getServer().getPluginManager());
    24. }
    25.  
    26. private void setupVault(PluginManager pm) {
    27. Plugin vault = pm.getPlugin("Vault");
    28. if (vault != null && vault instanceof net.milkbowl.vault.Vault) {
    29. console.sendMessage("Loaded Vault v" + vault.getDescription().getVersion());
    30. setupEconomy();
    31. }
    32. }
    33.  
    34. private boolean setupEconomy(){
    35. if (Bukkit.getPluginManager().getPlugin("Vault") == null) { return false; }
    36. console.sendMessage("Currently trying to find an economy..");
    37. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
    38. if (economyProvider != null) {
    39. econ = economyProvider.getProvider();
    40. } else {
    41. console.sendMessage("Failed to find an economy!");
    42. return false;
    43. }
    44. console.sendMessage("Economy enabled!");
    45. return (econ != null);
    46. }
    47. }


    Events Class
    Code:java
    1. package me.Jaaakee224.VillagerClick;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Villager;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerInteractEntityEvent;
    8. import static me.Jaaakee224.VillagerClick.VillagerClick.*;
    9.  
    10. public class Events implements Listener {
    11.  
    12. @EventHandler
    13. public void onPlayerClickVillager(PlayerInteractEntityEvent e) {
    14. if(e.getRightClicked() instanceof Villager) {
    15. Villager v = (Villager) e.getRightClicked();
    16. String vName = v.getCustomName();
    17. if(vName.equalsIgnoreCase("Credits")) {
    18. e.getPlayer().sendMessage(ChatColor.GREEN + "You currently have " + econ.getBalance(e.getPlayer().getName() + " Credits."));
    19.  
    20. }
    21. }
    22. }
    23. }


    Please help, all this code is the only code in the entire project. Thank you :)
     
  2. Offline

    ZapChance

    From what I can tell, your errors are on line 21 & line 28 of "VillagerClick"

    Code:java
    1. Line 21: Bukkit.getServer().getPluginManager().registerEvents((Listener) new Events(), this);
    2. Line 28: if (vault != null && vault instanceof net.milkbowl.vault.Vault) {


    I am led to think that you're not properly pointing to your listener class, which would throw the NullPointer, and that you aren't correctly looking for Vault, but, then again, I'm not the best with external plugin, so, don't take my word.
     
  3. Offline

    Jaaakee224

    ZapChance What do you think can provide a fix?
     
  4. Offline

    ZapChance

    Jaaakee224

    Don't cast listener, and add this to your event class.

    Code:text
    1. private mainclass plugin;
    2.  
    3. public eventclass(mainclass plugin) {
    4. this.plugin = plugin;
    5. }
     
  5. Offline

    Jaaakee224

    ZapChance Doesn't fix it and I get the same error as before.

    ZapChance Also, removing the cast to Listener will make it so the event won't work at all.

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

    captianwolfkid

    @Jaaakee224
    try this:
    In Events put
    Code:java
    1. public Events(VillagerClick plugin) {
    2. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    3. }

    and then replace that statement on line 21 with
    Code:java
    1. new Events(this);

    You don't need a cast, if it implements it already is an instance of Listener.

    That's the way I do it.
    Also, replace the net.milkbowl.vault.Vault with Vault and import net.milkbowl.vault.Vault (
    Code:java
    1. import net.milkbowl.vault.Vault;
    )
     
  7. Offline

    Jaaakee224

    captianwolfkid

    If I used
    Code:java
    1. import net.milkbowl.vault.Vault;


    What would I do with line 15 & 37
    Code:java
    1. public static Economy econ = null;

    Code:java
    1. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);


    Once I use the import you said it gives me an error on those lines by Eclipse.
     
  8. Offline

    boysnnoco

    Jaaakee224
    try to setup your economy first then register your events
     
  9. Offline

    Jaaakee224

  10. Offline

    mmiillkkaa

    You never initialize "console."
     
  11. Offline

    Jaaakee224

    mmiillkkaa this?
    Code:java
    1. public ConsoleCommandSender console;
     
  12. Offline

    mmiillkkaa

  13. Offline

    Jaaakee224

    mmiillkkaa I don't think its that because I use it with other plugins and it works fine.
     
  14. Offline

    mmiillkkaa

    Jaaakee224 It is not initialized and is therefore null. I recommend you replace it with Logger.
     
  15. Offline

    Jaaakee224

    mmiillkkaa

    Does this look good?
    Code:java
    1. package me.Jaaakee224.VillagerClick;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import net.milkbowl.vault.economy.Economy;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.Plugin;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.RegisteredServiceProvider;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class VillagerClick extends JavaPlugin {
    15.  
    16. public static Economy econ = null;
    17. public Logger log;
    18.  
    19.  
    20. @Override
    21. public void onEnable() {
    22. Bukkit.getServer().getPluginManager().registerEvents((Listener) new Events(), this);
    23.  
    24. setupVault(getServer().getPluginManager());
    25. }
    26.  
    27. private void setupVault(PluginManager pm) {
    28. Plugin vault = pm.getPlugin("Vault");
    29. if (vault != null && vault instanceof net.milkbowl.vault.Vault) {
    30. log.info("Loaded Vault v" + vault.getDescription().getVersion());
    31. setupEconomy();
    32. }
    33. }
    34.  
    35. private boolean setupEconomy(){
    36. if (Bukkit.getPluginManager().getPlugin("Vault") == null) { return false; }
    37. log.info("Currently trying to find an economy..");
    38. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
    39. if (economyProvider != null) {
    40. econ = economyProvider.getProvider();
    41. } else {
    42. log.info("Failed to find an economy!");
    43. return false;
    44. }
    45. log.info("Economy enabled!");
    46. return (econ != null);
    47. }
    48. }
     
  16. Offline

    mmiillkkaa

  17. Offline

    Jaaakee224

  18. Offline

    mmiillkkaa

  19. Offline

    Jaaakee224

    mmiillkkaa Still getting the same error though.
     
  20. Offline

    mmiillkkaa

    Jaaakee224 Then you didn't give log a value. Post your latest code.
     
  21. Offline

    Jaaakee224

    mmiillkkaa I don't see a big reason to put a value on Log, I've used the same code for other plugins which have worked.

    Latest Code
    Code:java
    1. package me.Jaaakee224.VillagerClick;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import net.milkbowl.vault.economy.Economy;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.Plugin;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.RegisteredServiceProvider;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class VillagerClick extends JavaPlugin {
    15.  
    16. public static Economy econ = null;
    17. public static Logger log;
    18.  
    19.  
    20. @Override
    21. public void onEnable() {
    22. Bukkit.getServer().getPluginManager().registerEvents((Listener) new Events(), this);
    23.  
    24. setupVault(getServer().getPluginManager());
    25. log = this.getLogger();
    26. }
    27.  
    28. private void setupVault(PluginManager pm) {
    29. Plugin vault = pm.getPlugin("Vault");
    30. if (vault != null && vault instanceof net.milkbowl.vault.Vault) {
    31. log.info("Loaded Vault v" + vault.getDescription().getVersion());
    32. setupEconomy();
    33. }
    34. }
    35.  
    36. private boolean setupEconomy(){
    37. if (Bukkit.getPluginManager().getPlugin("Vault") == null) { return false; }
    38. log.info("Currently trying to find an economy..");
    39. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
    40. if (economyProvider != null) {
    41. econ = economyProvider.getProvider();
    42. } else {
    43. log.info("Failed to find an economy!");
    44. return false;
    45. }
    46. log.info("Economy enabled!");
    47. return (econ != null);
    48. }
    49. }
     
  22. Offline

    mmiillkkaa

    Jaaakee224 You use log before you set its value. Put
    Code:java
    1. log = this.getLogger()
    at the top of onEnable instead of at the bottom.
     
  23. Offline

    Jaaakee224

  24. Offline

    mmiillkkaa

  25. Offline

    Jaaakee224

    mmiillkkaa seems to load properly, but this happens
    Code:
    11:38:24 AM [INFO] [VillagerClick] Enabling VillagerClick v1.0
    11:38:24 AM [INFO] [VillagerClick] Loaded Vault v1.2.31-b411
    11:38:24 AM [INFO] [VillagerClick] Currently trying to find an economy..
    11:38:24 AM [INFO] [VillagerClick] Failed to find an economy!
     
  26. Offline

    mmiillkkaa

    Jaaakee224 Do you have an economy plugin installed?
     
  27. Offline

    Jaaakee224

  28. Offline

    mmiillkkaa

  29. Offline

    Jaaakee224

  30. Offline

    mmiillkkaa

    Jaaakee224 Make sure that plugin is setting up the vault connector properly before you attempt to connect to the economy. Check if the plugin (GemEconomy) is already loaded. If not, wait for it to be loaded. You can listen to the PluginEnableEvent and check if the plugin being enabled is GemEconomy. After you find the plugin has been enabled, setup the economy.
     
Thread Status:
Not open for further replies.

Share This Page