NullPointerException?!?! Clearly Initialized.

Discussion in 'Plugin Development' started by waco001, Jul 20, 2013.

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

    waco001

    Hey guys, I'm working on a small game to drop anvils but this error

    Code:
    2013-07-20 18:46:45 [INFO] waco001 issued server command: /reload
    2013-07-20 18:46:45 [INFO] [anvildrop] Disabling anvildrop v0.1
    2013-07-20 18:46:45 [INFO] [anvildrop] SAMPLE ANVIL DROP DISABLED
    2013-07-20 18:46:45 [INFO] [anvildrop] Loading anvildrop v0.1
    2013-07-20 18:46:45 [INFO] [anvildrop] Enabling anvildrop v0.1
    2013-07-20 18:46:45 [INFO] [anvildrop] SAMPLE ANVIL DROP ENABLED
    2013-07-20 18:46:45 [INFO] ADMIN PLEASE SET POSITION!
    2013-07-20 18:46:45 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2013-07-20 18:46:45 [INFO] waco001: Reload complete.
    2013-07-20 18:46:48 [INFO] waco001 issued server command: /ad play
    2013-07-20 18:46:48 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ad' in plugin anvildrop v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:964)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:882)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:839)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:118)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
        at me.waco001.anvildrop.sample.Game.<init>(Game.java:19)
        at me.waco001.anvildrop.sample.Main.onCommand(Main.java:45)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    I really don't know what can be causing this. Ive looked online and I still can find a solution.

    Command of Main.java:

    Code:java
    1. }else if(args[0].equalsIgnoreCase("play")){
    2. Game game = new Game(player, 5); //THIS IS LINE 45


    And Game.java:

    Code:java
    1. package me.waco001.anvildrop.sample;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.FallingBlock;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.Inventory;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.util.Vector;
    13.  
    14. public class Game {
    15. Main main;
    16.  
    17. public boolean running = false;
    18. private int levels;
    19. private int time = main.getConfig().getInt("time");
    20. private ItemStack[] items;
    21. private Location locationArena = new Location(
    22. Bukkit.getServer().getWorld(main.getConfig().getString("LocationArena.world")),
    23. main.getConfig().getDouble("LocationArena.x"),
    24. main.getConfig().getDouble("LocationArena.y"),
    25. main.getConfig().getDouble("LocationArena.z"));
    26.  
    27. private double pitch = (0 * Math.PI) / 180;
    28. private double yaw = (22 * Math.PI) / 180;
    29. private double x = Math.sin(pitch) * Math.cos(yaw);
    30. private double y = Math.sin(pitch) * Math.sin(yaw);
    31. private double z = Math.cos(pitch);
    32. Vector vector = new Vector(x, z + 10, y);
    33.  
    34. public Game(Player player, int inlevels){
    35. play(player, inlevels);
    36. }
    37.  
    38. public void play(Player player, int inLevels){
    39. running = true;
    40. levels = inLevels;
    41. if(!(main.setup)){
    42. exit("Add Position To Config", player);
    43.  
    44. }
    45. player.teleport(locationArena);
    46. loadinventory(player);
    47. gameplay(player, levels);
    48.  
    49. }
    50.  
    51. public void exit(String string, Player p) {
    52. restoreinventory(p);
    53. running = false;
    54. }
    55. private void loadinventory(Player p){
    56. items = p.getInventory().getContents();
    57. p.getInventory().setContents(null);
    58. p.getInventory().setArmorContents(null);
    59. }
    60. private void restoreinventory(Player p){
    61. p.getInventory().setContents(items);
    62. }
    63.  
    64. private void gameplay(Player player, int i){
    65. boolean alive = true;
    66. while(alive){
    67. FallingBlock block = player.getLocation().getWorld().spawnFallingBlock(player.getLocation(), Material.ANVIL, (byte) 0x0);
    68. block.setVelocity(vector);
    69. if(player.getLocation() == block.getLocation()){
    70. alive = false;
    71. }
    72. }
    73. }
    74. private void reward(int number){
    75.  
    76. ItemStack itemstack = new ItemStack(Material.GOLD_INGOT, number);
    77.  
    78. }
    79. }
    80.  


    It would mean alot if someone could help me. Thanks Alot Guys!
    waco
     
  2. Offline

    adam753

    Code:
    private int time = main.getConfig().getInt("time");
    
    You're giving a field variable a value. This means that the variable "time" will be assigned that value as soon as the class is instantiated. The problem you're having is that when the class instance is being created, all its variables are null - for example, you haven't yet had a chance to set its "main" variable, hence why this line is giving an error.
    Simply put, fix the problem by not doing this. Instead, set the value of time after setting others (so probably at the end of a constructor or something).
     
  3. Offline

    waco001

    Even after removing the time, there is the same error. Does any variable from Main have to be in the initialized class... public Game?
     
  4. Offline

    adam753

    Basically the problem is that you are referring to main before giving it a value, which should be the instance of your main class. Change line 45 of Main to this:
    Code:
    Game game = new Game(player, 5, this);
    
    And then change Game's constructor so that it takes an instance of Main, sets its own main variable with it, and then sets the time value after that.
     
  5. Offline

    waco001

    Man, no matter what I do, it just wont work :/ I've changed it to this so far like you've said... But it still shows the NullPointer
    Code:java
    1.  
    2. public class Game {
    3.  
    4. public boolean running = false;
    5. private int levels;
    6. private ItemStack[] items;
    7. private double pitch = (0 * Math.PI) / 180;
    8. private double yaw = (22 * Math.PI) / 180;
    9. private double x = Math.sin(pitch) * Math.cos(yaw);
    10. private double y = Math.sin(pitch) * Math.sin(yaw);
    11. private double z = Math.cos(pitch);
    12. Vector vector = new Vector(x, z + 10, y);
    13.  
    14. public Game(Player player, int inlevels, Main main){
    15. play(player, inlevels);
    16. }
    17.  
    18. Main main;
    19. private Location locationArena = new Location(
    20. Bukkit.getServer().getWorld(main.getConfig().getString("LocationArena.world")),
    21. main.getConfig().getDouble("LocationArena.x"),
    22. main.getConfig().getDouble("LocationArena.y"),
    23. main.getConfig().getDouble("LocationArena.z"));
    24. public void play(Player player, int inLevels){
    25. running = true;
    26. levels = inLevels;
    27. if(!(main.setup)){
    28. exit("Add Position To Config", player);
    29.  
    30. }
    31. player.teleport(locationArena);
    32. loadinventory(player);
    33. gameplay(player, levels);
    34.  
    35. }
     
  6. Offline

    adam753

    Because you still aren't setting Game's main variable.
    Code:java
    1.  
    2. public Game(Player player, int inlevels, Main main){
    3. play(player, inlevels);
    4. this.main = main; //Add this line
    5. }
    6.  
     
  7. Offline

    waco001

    Even with that
    >.>

    -_- Now it wont even enable when I reload...
    Code:
    2013-07-20 23:26:18 [INFO] waco001 issued server command: /reload
    2013-07-20 23:26:18 [INFO] [anvildrop] Disabling anvildrop v0.1
    2013-07-20 23:26:18 [INFO] [anvildrop] SAMPLE ANVIL DROP DISABLED
    2013-07-20 23:26:18 [SEVERE] Could not load 'plugins\me.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.reload(CraftServer.java:603)
        at org.bukkit.Bukkit.reload(Bukkit.java:275)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:964)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:882)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:839)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:118)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
        at me.waco001.anvildrop.sample.Game.<init>(Game.java:22)
        at me.waco001.anvildrop.sample.Main.<init>(Main.java:14)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 21 more
    2013-07-20 23:26:18 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2013-07-20 23:26:18 [INFO] waco001: [0;32;1mReload complete.[m
    2013-07-20 23:26:22 [INFO] waco001 issued server command: /ad play
    2013-07-20 23:26:28 [INFO] waco001 issued server command: /ad play
    2013-07-20 23:26:33 [INFO] waco001 issued server command: /ad reload
    2013-07-20 23:26:52 [INFO] waco001 issued server command: /reload
    2013-07-20 23:26:52 [SEVERE] Could not load 'plugins\me.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.reload(CraftServer.java:603)
        at org.bukkit.Bukkit.reload(Bukkit.java:275)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:964)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:882)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:839)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:118)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
        at me.waco001.anvildrop.sample.Game.<init>(Game.java:22)
        at me.waco001.anvildrop.sample.Main.<init>(Main.java:14)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 21 more
    I have just added
    Code:java
    1. private Game game = new Game(this);
    in Main and I've added

    Code:java
    1. Main plugin;
    2.  
    3. public Game(Main instance) {
    4. this.plugin = instance; //now you can access plugin
    5. }


    Please Help Me, Im getting frustrated... Alot. Thanks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 15, 2022
  8. Offline

    xTrollxDudex

    waco001
    Show the main class

    Edit: the Game(Main instance) constructor goes in Game class. You're would also put Main plugin too.
    The in the main class you would have a new Game(this) somewhere
     
  9. Offline

    waco001

    Oh Gawd Im so confused this is my Main:
    Code:java
    1. package me.waco001.anvildrop.sample;
    2.  
    3. import java.io.File;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public final class Main extends JavaPlugin {
    12. static Logger log = Logger.getLogger("Minecraft");
    13. public boolean setup;
    14. private Game game = new Game(this);
    15.  
    16.  
    17.  
    18. @Override
    19. public void onEnable(){
    20. getLogger().info("SAMPLE ANVIL DROP ENABLED");
    21. if (!new File(getDataFolder(), "config.yml").exists()) {
    22. saveDefaultConfig();
    23. }
    24. if(Main.this.getConfig().getString("position") != null){
    25. setup = true;
    26. }else{ log.info("ADMIN PLEASE SET POSITION!"); }
    27. }
    28.  
    29. @Override
    30. public void onDisable() {
    31. getLogger().info("SAMPLE ANVIL DROP DISABLED");
    32. this.saveConfig();
    33. }
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. if(cmd.getName().equalsIgnoreCase("anvildrop") || cmd.getName().equalsIgnoreCase("ad")){
    36. if (!(sender instanceof Player)) {
    37. sender.sendMessage("error playersonly");
    38. return true;
    39. }
    40. Player player = (Player) sender;
    41.  
    42. if (args.length == 0) {
    43. return false;
    44. }else{
    45. if(args[0].equalsIgnoreCase("reload")){
    46. this.saveConfig();
    47. }else if(args[0].equalsIgnoreCase("play")){
    48.  
    49. }else if(args[0].equalsIgnoreCase("config")){
    50. player.sendMessage("HAI");
    51. if(args[1].equalsIgnoreCase("setarena")){
    52. this.getConfig().set("locationArena.world", player.getWorld().getName());
    53. this.getConfig().set("locationArena.x", player.getLocation().getX());
    54. this.getConfig().set("locationArena.y", player.getLocation().getY());
    55. this.getConfig().set("locationArena.z", player.getLocation().getZ());
    56. player.sendMessage("Location Arena Points Have Been Set.");
    57. return true;
    58. }else{
    59. player.sendMessage("PWAAHH");
    60. }
    61. }
    62. }
    63. }
    64. return false;
    65. }
    66.  
    67. }

    This is my Game Class
    Code:java
    1. package me.waco001.anvildrop.sample;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.FallingBlock;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.util.Vector;
    10.  
    11. public class Game {
    12. Main plugin;
    13.  
    14. public Game(Main instance) {
    15. this.plugin = instance; //now you can access plugin
    16. }
    17. public boolean running = false;
    18. private int levels;
    19. //private int time = main.getConfig().getInt("time");
    20. private ItemStack[] items;
    21. private Location locationArena = new Location(
    22. Bukkit.getServer().getWorld(plugin.getConfig().getString("LocationArena.world")),
    23. plugin.getConfig().getDouble("LocationArena.x"),
    24. plugin.getConfig().getDouble("LocationArena.y"),
    25. plugin.getConfig().getDouble("LocationArena.z"));
    26.  
    27. private double pitch = (0 * Math.PI) / 180;
    28. private double yaw = (22 * Math.PI) / 180;
    29. private double x = Math.sin(pitch) * Math.cos(yaw);
    30. private double y = Math.sin(pitch) * Math.sin(yaw);
    31. private double z = Math.cos(pitch);
    32. Vector vector = new Vector(x, z + 10, y);
    33.  
    34. public void play(Player player, int inLevels){
    35. running = true;
    36. levels = inLevels;
    37. if(!(plugin.setup)){
    38. exit("Add Position To Config", player);
    39.  
    40. }
    41. //player.teleport(locationArena);
    42. loadinventory(player);
    43. gameplay(player, levels);
    44.  
    45. }
    46.  
    47. public void exit(String string, Player p) {
    48. restoreinventory(p);
    49. running = false;
    50. }
    51. private void loadinventory(Player p){
    52. items = p.getInventory().getContents();
    53. p.getInventory().setContents(null);
    54. p.getInventory().setArmorContents(null);
    55. }
    56. private void restoreinventory(Player p){
    57. p.getInventory().setContents(items);
    58. }
    59.  
    60. private void gameplay(Player player, int i){
    61. boolean alive = true;
    62. while(alive){
    63. FallingBlock block = player.getLocation().getWorld().spawnFallingBlock(player.getLocation(), Material.ANVIL, (byte) 0x0);
    64. block.setVelocity(vector);
    65. if(player.getLocation() == block.getLocation()){
    66. alive = false;
    67. }
    68. }
    69. }
    70. private void reward(int number){
    71.  
    72. ItemStack itemstack = new ItemStack(Material.GOLD_INGOT, number);
    73.  
    74. }
    75. }

    Ive added
    Main plugin;

    public Game(Main instance) {
    this.plugin = instance; //now you can access plugin
    } in Game.java but it doesnt work at all.
     
  10. Offline

    xTrollxDudex

    waco001
    Dude, you are VERY close. Game game = new Game(this) creates a new instance of game, what you want to do is just initialize the class game. Use
    PHP:
    new Game(this);
    In the place of private Game game = new Game(this);. Remove the private access modifier, and dont change ANY of the code I made.

    Simple right? It should work.
     
  11. Offline

    waco001

    Nothing is work -_-
    It doesn't even Enable anymore
     
  12. Offline

    xTrollxDudex

    waco001
    If it doesnt even enable thats an export problem.
    Otherwise, errors? Can I see your current code as of now?
     
  13. Offline

    waco001

    No, I learned that the errors are from the config not loading fully or something.

    "Caused by: java.lang.IllegalArgumentException: Name cannot be null
    at org.apache.commons.lang.Validate.notNull(Validate.java:203)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.getWorld(CraftServer.java:819)
    at org.bukkit.Bukkit.getWorld(Bukkit.java:247)
    at me.waco001.anvildrop.sample.Main.onCommand(Main.java:54)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more"
    Line 54:
    Bukkit.getWorld(config.getString("LocationArena.world")),
    Main.java: http://pastie.org/8164925
    Game.java: http://pastie.org/8164926
    Thanks for your help @xTrollxDudex

    Wow Guys, I found the problem I was having. When I was saving the coordinates, I capitalized the first letter of the word in the address in the config file... LOL this was a really stupid mistake that could've saved me hours of frustration if I caught it earlier! Thanks For Your Help Guys,
    waco001

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page