Solved Team mechanics in snowball fight plugin throws NPE

Discussion in 'Plugin Development' started by Hobbit9797, Feb 24, 2013.

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

    Hobbit9797

  2. Offline

    gamerzap

    You should set el.spiel to spiel instead of making another instance of Spiel, but I'm not sure if that is what's causing your problem.
    EDIT: Actually, I think it is causing your problem, because a new spiel would not have it's hsf initialized, so it would generate an NPE.
     
  3. You don't really understand what "new" does do you ? It's not a magic word to get the class you need, it creates a new instance of it, a clone, a copy, something like it but totally diferent !
    You should read some tutorials about Java instances.

    This is how it should be:

    Code:
    // main class
    
    public void onEnable() {
      // create one instance of other classes and store them
      spiel = new Spiel();
      el = new EntityListener(this, spiel);
    
      // register the entity listener we stored...
      getServer().getPluginManager().registerEvents(el, this);
    }
    
    // entity listener class
    
    public class EntityListener implements Listener {
      private Spiel spiel;
      private HobbitSnowFight hsf;
    
      public EntityListener(HobbitSnowFight hsf, Spiel spiel) {
        this.hsf = hsf;
        this.spiel = spiel;
      }
    
      // events and stuff...
    }
    Also, don't use Player as hashmap key, use player's name as String instead.

    Next, you're casting the entity to SnowBall *before* you're checking it, that will give you a nice error when something hits something else with anything but snowball...
     
  4. Offline

    Hobbit9797

    OMG you guys are awesome!
     
Thread Status:
Not open for further replies.

Share This Page