Getting a NullPointerException, and not to sure why.

Discussion in 'Plugin Development' started by bkleinman1, Aug 30, 2014.

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

    bkleinman1

    I am trying to set values to the config file from a class other than the one that extends JavaPlugin. I had this working in a different project, and have practically copied the code over from the other project, yet it will not work in this.

    I first did this in my Main class: (Removing any code not related to this question)
    Code:java
    1. public class Main extends JavaPlugin {
    2. public static Plugin plugin = null;
    3.  
    4. @Override
    5. public void onEnable() {
    6. plugin = this;
    7. }
    8. }


    Then trying to access the config file from another class, did this: ('name' being a string)
    Code:java
    1. Main.plugin.getConfig().set("Mines." + name + ".x1", GetRegion.pos1.getX());



    I have tried outputting the 'GetRegion.pos1.getX()' and it returned the correct value, therefore, believe my problem here is that something to do with accessing the Main class is null. I cannot however figure out why, or what I can do to fix this.

    Thanks in advance for any help anyone can offer me, and I apologise if the code examples I have given were not enough for you to help, ask, and I shall provide what you need.
     
  2. Offline

    TomTheDeveloper

  3. Offline

    bkleinman1

    TomTheDeveloper
    org.bukkit.command.CommandException: Unhandled exception executing command 'mine
    s' in plugin AkrillsPlugin v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.NullPointerException
    at me.bkleinman1.Akrill.Commands.command(Commands.java:46) ~[?:?]
    at me.bkleinman1.Akrill.Main.onCommand(Main.java:37) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    ... 13 more
     
  4. Offline

    Necrodoom

    bkleinman1 paste full class and main class please.
     
  5. Offline

    Tecno_Wizard

    bkleinman1, don't use
    1. Main.plugin.getConfig().set("Mines." + name + ".x1", GetRegion.pos1.getX());
    Use
    1. Main.getConfig().set("Mines." + name + ".x1", GetRegion.pos1.getX());
    and see if that fixes your problem.
    Also, I do not know of a java method that begins with a capital letter, check on that (GetRegion)
     
  6. Offline

    bkleinman1

    Necrodoom

    Main class:
    Code:java
    1. package me.bkleinman1.Akrill;
    2.  
    3. import me.bkleinman1.Akrill.Prison.Mechanics.AreaSelection;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.plugin.Plugin;
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11.  
    12. public class Main extends JavaPlugin {
    13. public static Plugin plugin = null;
    14. public final Commands commands = new Commands();
    15. public final AreaSelection as = new AreaSelection();
    16.  
    17. int numOfMines = 0;
    18.  
    19. @Override
    20. public void onEnable() {
    21. plugin = this;
    22. PluginManager pm = getServer().getPluginManager();
    23. pm.registerEvents(this.as, this);
    24.  
    25. getConfig().options().copyDefaults(true);
    26. saveDefaultConfig();
    27.  
    28. numOfMines = getConfig().getInt("NumberOfMines");
    29. }
    30.  
    31. @Override
    32. public void onDisable() {
    33. getConfig().set("NumberOfMines", numOfMines);
    34. }
    35.  
    36. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    37. Player player = (Player) sender;
    38. if (commandLabel.equalsIgnoreCase("mines")) {
    39. if (args.length == 0) {
    40. player.sendMessage(prisonPrefix + "LIST MINES COMMANDS HERE!!!");
    41. }
    42. if (args.length == 1) {
    43. if (args[0].equalsIgnoreCase("create")) {
    44. player.sendMessage(prisonPrefix + "Incorrect syntax: You are missing the name of the mine!");
    45. }
    46. if (args[0].equalsIgnoreCase("reset")) {
    47. for (int i = 0; i < main.numOfMines; i++) {
    48. Location loc = as.getLocationFromYmal(minesList[i], player);
    49. loc.getBlock().setType(Material.DIAMOND_BLOCK);
    50. }
    51. Bukkit.broadcastMessage(prisonPrefix + "The mines have reset!");
    52. }
    53. }
    54. if (args.length == 2) {
    55. if (args[0].equalsIgnoreCase("create")) {
    56. if (GetRegion.pos1 != null && GetRegion.pos2 != null) {
    57. as.locationToYmal(args[1]);
    58. Main.plugin.getConfig().set("MinesList", Arrays.asList(minesList));
    59. main.numOfMines++;
    60. player.sendMessage(prisonPrefix + "The mine has been created.");
    61. } else {
    62. player.sendMessage(prisonPrefix + "Positions are null.");
    63. }
    64. }
    65. }
    66.  
    67. }
    68. return true;
    69. }
    70.  
    71. }
    72. [/i]


    Other class:
    Code:java
    1. package me.bkleinman1.Akrill.Prison.Mechanics;
    2.  
    3. import me.bkleinman1.Akrill.Main;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13.  
    14. public class AreaSelection implements Listener {
    15. public static Main main;
    16. public GetRegion gr = new GetRegion();
    17.  
    18. @EventHandler
    19. public void onPlayerInteract(PlayerInteractEvent e) {
    20.  
    21. Player player = e.getPlayer();
    22.  
    23. if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
    24. if (e.getPlayer().getItemInHand().getType() == Material.ARROW) {
    25. GetRegion.pos1 = e.getClickedBlock().getLocation();
    26. player.sendMessage(ChatColor.BLUE + "[Prison] " + ChatColor.AQUA + "pos1 has been set to: " + ChatColor.GRAY + GetRegion.pos1.getX() + ", " + GetRegion.pos1.getY() + ", " + GetRegion.pos1.getY());
    27. e.setCancelled(true);
    28. }
    29. }
    30.  
    31. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    32. if (e.getPlayer().getItemInHand().getType() == Material.ARROW) {
    33. GetRegion.pos2 = e.getClickedBlock().getLocation();
    34. player.sendMessage(ChatColor.BLUE + "[Prison] " + ChatColor.AQUA + "pos2 has been set to: " + ChatColor.GRAY + GetRegion.pos2.getX() + ", " + GetRegion.pos2.getY() + ", " + GetRegion.pos2.getY());
    35. e.setCancelled(true);
    36. }
    37. }
    38.  
    39. }
    40.  
    41.  
    42. public void locationToYmal(String name) {
    43. Main.plugin.getConfig().set("Mines." + name + ".x1", GetRegion.pos1.getX());
    44. Main.plugin.getConfig().set("Mines." + name + ".y1", GetRegion.pos1.getY());
    45. Main.plugin.getConfig().set("Mines." + name + ".z1", GetRegion.pos1.getZ());
    46. Main.plugin.getConfig().set("Mines." + name + ".x2", GetRegion.pos2.getX());
    47. Main.plugin.getConfig().set("Mines." + name + ".y2", GetRegion.pos2.getY());
    48. Main.plugin.getConfig().set("Mines." + name + ".z2", GetRegion.pos2.getZ());
    49.  
    50. }
    51.  
    52. public Location getLocationFromYmal(String name, Player p) {
    53. double x = Main.plugin.getConfig().getDouble("Mines." + name + ".x");
    54. double y = Main.plugin.getConfig().getDouble("Mines." + name + ".y");
    55. double z = Main.plugin.getConfig().getDouble("Mines." + name + ".z");
    56. Location loc = new Location(p.getWorld(), x, y, z);
    57. return loc;
    58. }
    59. }
    60.  


    Tecno_Wizard I have tried that before, it did not work
     
  7. Offline

    Necrodoom

    bkleinman1 first off, get rid of the static plugin, use instances. Also, why are you referencing the main class static, then go to a variable, while on the main class it self, when you could've used 'this'.

    Also, what is GetRegion? You construct this object, then try to reference it statically, but its not static. Use your variables properly.
     
Thread Status:
Not open for further replies.

Share This Page