NullPointException, Why?

Discussion in 'Plugin Development' started by Doubtstand, Sep 20, 2015.

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

    Doubtstand

    My only question is why I get a NullPointException on the line getConfig().set and getConfig().getDouble, and how I can fix it.

    SetSpawn:
    Code:java
    1.  
    2. public class SetSpawnCommand implements CommandExecutor{
    3.  
    4. Main plugin;
    5.  
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    7. if(!(sender instanceof Player)){
    8. sender.sendMessage(plugin.playeronly);
    9. return true;
    10. }
    11.  
    12. Player player = (Player) sender;
    13.  
    14. if(cmd.getName().equalsIgnoreCase("setspawn")){
    15. if(!player.hasPermission("spawn.set")){
    16. player.sendMessage(plugin.nopermission);
    17. return true;
    18. }
    19. if(args.length == 0){
    20.  
    21. plugin.getConfig().set("Spawn.X", player.getLocation().getBlockX());
    22. plugin.getConfig().set("Spawn.Y", player.getLocation().getBlockY());
    23. plugin.getConfig().set("Spawn.Z", player.getLocation().getBlockZ());
    24. plugin.getConfig().set("Spawn.World", player.getLocation().getWorld().getName());
    25. plugin.saveConfig();
    26.  
    27. player.sendMessage(ChatColor.GRAY + "The Spawn has been set on your location!");
    28. return true;
    29. }
    30. if(args.length >= 1){
    31. player.sendMessage(ChatColor.RED + "Too many arguments, please use /setspawn.");
    32. return true;
    33. }
    34. }
    35. return true;
    36. }
    37. }
    38.  
    39.  


    Spawn:
    Code:java
    1.  
    2. public class SpawnCommand implements CommandExecutor{
    3.  
    4. Main plugin;
    5.  
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    7. if(!(sender instanceof Player)){
    8. sender.sendMessage(plugin.playeronly);
    9. return true;
    10. }
    11.  
    12. Player player = (Player) sender;
    13.  
    14. if(cmd.getName().equalsIgnoreCase("spawn")){
    15. if(plugin.getConfig().getConfigurationSection("Spawn") == null){
    16. player.sendMessage(ChatColor.GRAY + "There is no spawn set!");
    17. return true;
    18. }
    19.  
    20. World w = Bukkit.getServer().getWorld(plugin.getConfig().getString("Spawn.World"));
    21. double x = plugin.getConfig().getDouble("Spawn.X");
    22. double y = plugin.getConfig().getDouble("Spawn.Y");
    23. double z = plugin.getConfig().getDouble("Spawn.Z");
    24.  
    25. player.teleport(new Location(w, x, y, z));
    26.  
    27. player.sendMessage(ChatColor.GRAY + "You have been teleported to spawn.");
    28. return true;
    29. }
    30. return true;
    31. }
    32. }
    33.  
    34.  
     
  2. Offline

    ShadowLAX

    @Doubtstand The field 'plugin' hasn't been initialized, so it and any getter methods will return null.
     
  3. Offline

    Doubtstand

    How can I intialize it?
     
  4. Offline

    Zombie_Striker

    @Doubtstand
    You set it to something?

    Did you learn Java? This is something very basic.
     
  5. Offline

    Doubtstand

    I intialized it, but when I do /spawn, It teleports me on the corner of 4 blocks, I want the player to spawn on the block itself, how?
     
  6. Offline

    Zombie_Striker

  7. Offline

    teej107

    The methods won't return anything. The methods won't even be called because it's on a null Object.
     
  8. Offline

    CentrumGuy

    Initialize plugin: Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("plugin name");

    also if you want the location to be exact do: double x = p.getLocation().getX()

    To teleport to the middle of a block do:
    double x = p.getLocation().getBlockX() + 0.5;
    double y = p.getLocation().getBlockY();
    double z = p.getLocation().getBlockZ() + 0.5;
     
Thread Status:
Not open for further replies.

Share This Page