NullPointer on RespawnEvent

Discussion in 'Plugin Development' started by bballheat, Jul 2, 2013.

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

    bballheat

    I'm stuck on having my player respawn at a certain location. I have put together the following code but am getting a NullPointer on the int x line.
    Code:
        @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent p)
        {
            Player player = p.getPlayer();
            int x = plugin.getConfig().getInt("Plugin." + "Spawn." + "X");
            int y = plugin.getConfig().getInt("Plugin." + "Spawn." + "Y");
            int z = plugin.getConfig().getInt("Plugin." + "Spawn." + "Z");
            p.setRespawnLocation(new Location(player.getWorld(), x, y, z));
        }
     
  2. Offline

    MUG806

    Sounds like the config doesn't exist.
     
  3. Offline

    bballheat

    Nope it exists:
    Code:
    Plugin:
      Spawn:
        X: -114.5
        Y: 66
        Z: 239.5
     
  4. Offline

    MUG806

    I mean the Config object, not the config.yml
     
  5. Offline

    AmShaegar

    Based on your code we can only continue guessing. My turn:

    plugin is null.
     
    Rocoty likes this.
  6. Offline

    iFamasssxD

    You need to set a delayedTask in the respawn method. It seems to not work immediately and needs to be delayed. I had to do this on a plugin I made where it gave the player a potion effect on respawn.

    Code:
    @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent p)
        {
            final Player player = p.getPlayer();
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
            int x = plugin.getConfig().getInt("Plugin." + "Spawn." + "X");
            int y = plugin.getConfig().getInt("Plugin." + "Spawn." + "Y");
            int z = plugin.getConfig().getInt("Plugin." + "Spawn." + "Z");
            p.setRespawnLocation(new Location(player.getWorld(), x, y, z));
        }
     
        }, 20); // ******** The 20. is the delay for the task in TICKS
                            // ********
    }
     
  7. Offline

    AmShaegar

    This wont fix the NPE if plugin is null but move it up to your plugin.getServer()...
     
  8. Offline

    bballheat

    This didn't fix it :( AmShaegar How do I fix the plugin being null?
     
  9. Offline

    FireBreath14

    make a constructor

    Code:
    //pretend 'main' was your main class and //'thisclass' is the class you're having the NPE in
     
    main plugin;
    thisclass(main c){
    plugin=c;
    }
    
    that way when you run plugin.whatever, youre accessing the main config
     
  10. Offline

    iFamasssxD

    Add a plugin instance

    Code:
    Mainpluginnamehere plugin;
     
        public Listenernamehere(Mainpluginnamehere instance) {
            plugin = instance;
        }
     
  11. Offline

    bballheat

    Ok so I have multiple classes, a main and the event class. Where would this go? And is Mainpluginnamehere my Main class?
     
  12. Offline

    iFamasssxD

    Yes main class is Mainpluginnamehere and you can put this in any or all listener classes
     
  13. Offline

    bballheat

    Couldn't get that to work either.
     
  14. Offline

    AmShaegar

    Always follow the same scheme for Listeners:
    Code:
    public class PlayerListener implements Listener {
       
      private Plugin plugin;
       
      public PlayerListener(Plugin plugin) {
        this.plugin = plugin;
      }
    }
     
  15. Offline

    bballheat

    Ok I tried this but didn't work. I decided to take a break over night so I could think about it and now the nullpointer is on:
    Code:
    final Player player = p.getPlayer();
    EDIT: So doesn't that mean I should check to see if the entity respawning is a player?
     
  16. Offline

    AmShaegar

    Are you sure? Is p still the PlayerRespawnEvent? If so it cannotbe null. Bukkit does not pass null as event.
     
  17. Offline

    bballheat

    I tested that and it didn't work then I changed the timer thing to Bukkit.getServer() and I got an error saying plugin was null.

    Wait no. This is line 41, the error line: Oh and p is still the event
    Code:
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    Ok so now the lines with the errors are my x y z integers. I found this by replacing my x y z with actual numbers and when I died I was sent to those coordinates. So now I am 100% sure its something with that config.

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

Share This Page