Help with this NPE?

Discussion in 'Plugin Development' started by Ranil, Oct 23, 2013.

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

    Ranil

    I keep getting a Null Pointer Exception from this command:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("jointeam")) {
    2. Team team = th.smallerTeam();
    3. th.addPlayer(player, team);
    4. player.sendMessage("yay");
    5. }

    th is my Team Handler, and it's at line 85, which is the "smallerTeam" method, which is:
    Code:java
    1. public Team smallerTeam() {
    2. if (redTeam.getSize() > blueTeam.getSize()) {
    3. return redTeam;
    4. } else {
    5. return blueTeam;
    6. }
    7. }

    And the addPlayer method:
    Code:java
    1. public void addPlayer(Player player, Team team) {
    2. Team teamCheck = getTeam(player);
    3. if (team != null && teamCheck != null) {
    4. if (team == blueTeam) {
    5. blueTeam.addPlayer(player);
    6. if (teamCheck.equals(redTeam)) {
    7. removePlayer(player, redTeam);
    8. } else if (teamCheck.equals(observers)) {
    9. removePlayer(player, observers);
    10. }
    11. } else if (team == redTeam) {
    12. redTeam.addPlayer(player);
    13. if (teamCheck.equals(observers)) {
    14. removePlayer(player, observers);
    15. } else if (teamCheck.equals(blueTeam)) {
    16. removePlayer(player, blueTeam);
    17. }
    18. } else if (team == observers) {
    19. observers.addPlayer(player);
    20. if (teamCheck.equals(redTeam)) {
    21. removePlayer(player, redTeam);
    22. } else if (teamCheck.equals(blueTeam)) {
    23. removePlayer(player, blueTeam);
    24. }
    25. }
    26. }
    27. }

    As you can see from the addPlayer method, there's also removePlayer and getTeam. If you need those, let me know and I'll post them.

    Thanks!
     
  2. Offline

    Amgis

    I'd need to see your Team class to fix this issue.

    Also, do you have the error message I could see? Surely bukkit has printed out the line number where the error occurred.
     
  3. Offline

    Ranil


    The error
    Code:
    2013-10-23 22:46:26 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'jointeam' in plugin Sample Plugin v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
        at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at me.Ranil.pvp.commands.AdminCommands.onCommand(AdminCommands.java:85)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    Line 85:
    Code:java
    1. Team team = th.smallerTeam();

    That is inside of the 'jointeam' command.
    The TeamHandler class:
    Here.
     
  4. Offline

    Amgis

    What's likely is that getSize() is returning null, but I don't know why without seeing your Team class.
     
  5. Offline

    Ranil

    Everything pertaining to teams is that TeamHandler class that is at the bottom of my last post.

    If you didn't see it, here.
     
  6. Offline

    5pHiNxX

    @Ranil: did you initialize th with TeamHandler th = new TeamHandler(); before calling it?
     
  7. Offline

    Ranil

    Well, now I feel stupid. I did initialize it so there were no errors, but I didn't do it correctly. Thanks :p
     
Thread Status:
Not open for further replies.

Share This Page