Assinging teams not working :(

Discussion in 'Plugin Development' started by RainoBoy97, Dec 21, 2012.

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

    RainoBoy97

    Hai!

    Getting this error:
    Code:
    23:17:12 [SEVERE] Error occurred while enabling PvP v0.1 (Is it up to date?)
    java.lang.NullPointerException
            at me.rainoboy97.PvP.TeamSplitter.<init>(TeamSplitter.java:17)
            at me.rainoboy97.PvP.PvP.onEnable(PvP.java:18)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugin(CraftServer.java
    :278)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.enablePlugins(CraftServer.j
    ava:260)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.reload(CraftServer.java:587
    )
            at org.bukkit.Bukkit.reload(Bukkit.java:184)
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    6)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer
    .java:510)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchServerCommand(Craft
    Server.java:502)
            at net.minecraft.server.v1_4_6.DedicatedServer.al(DedicatedServer.java:2
    60)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:22
    5)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:49
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    With this poece of code:
    Code:
    package me.rainoboy97.PvP;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class TeamSplitter implements Listener {
     
    PvP plugin;
     
    public TeamSplitter(PvP instance){
    plugin = instance;
    }
     
    int redTeamSize = plugin.redTeam.size();
    int blueTeamSize = plugin.blueTeam.size();
     
     
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
    Player player = event.getPlayer();
    if(blueTeamSize == redTeamSize){
    plugin.redTeam.add(player.getName());
    player.setPlayerListName(ChatColor.RED + player.getName());
    }
    if(blueTeamSize >= redTeamSize){
    plugin.redTeam.add(player.getName());
    player.setPlayerListName(ChatColor.RED + player.getName());
    }
    if(redTeamSize >= blueTeamSize){
    plugin.blueTeam.add(player.getName());
    player.setPlayerListName(ChatColor.BLUE + player.getName());
    }
    }
    }
    Im using the latest 1.4.6 dev build..
    Cant seem to figure out the problem :s
     
  2. Offline

    Unscrewed

    Show me your onEnable()

    >> 23:17:12 [SEVERE] Error occurred while enabling PvP v0.1
     
  3. Offline

    RainoBoy97

    public void onEnable(){
    getServer().getPluginManager().registerEvents(new TeamSplitter(this), this);
    }
     
  4. Offline

    Tirelessly

    Read the stickied posts for "How to read your own stacktraces and troubleshoot your plugins"
     
  5. Offline

    RainoBoy97

    Well, I've read it a couple times before...
     
  6. Offline

    fireblast709

    You are using plugin before you initialize it
     
  7. Offline

    RainoBoy97

    In the TeamSplitter class?
     
  8. Offline

    fireblast709

    yes
     
  9. Offline

    RainoBoy97

    Hmm, I've always done it this way (i think) but now it dont work.
     
  10. Offline

    Tirelessly

    The global variable gets assigned before the constructor passes.
     
  11. Offline

    skipperguy12

    Heres how you should do it:


    private final {classname} plugin;
    public {listenername}({classname} plugin) {
    this.plugin = plugin;
    }


    Now you can access methods and everything else from it using: plugin.ect

    classname is the class you're making the constructor for

    listenername is the name of the class you're making all of this in (you're listener)

    Hope I helped!
     
  12. Offline

    fireblast709

    Derp read the op. He already has that, but he already used plugin (which is at that moment null) in the class body
     
  13. Offline

    RainoBoy97

    Tbh i dont see why its null, ive used it after the constructor.
     
  14. Offline

    fireblast709

    No you use it before the constructor is called.
     
  15. Offline

    RainoBoy97

    But if i assign the PvP plugin; variable after the constructor the constructor wouldnt work?
     
  16. Offline

    fireblast709

    no because when you create the object, it first initializes ALL the fields (including the assignment, if it is present. So yes, it accesses the plugin variable here), then it calls the constructor.
    You can fix this by moving the actual assignment (what is after the '=') into the constructor (so after the 'this.plugin = plugin' line)
     
Thread Status:
Not open for further replies.

Share This Page