Solved Variable not being initialized onEnable

Discussion in 'Plugin Development' started by tommycake50, Jul 25, 2013.

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

    tommycake50

    Ok, i usually don't post here because im not so much of a noob as i used to be ;) lol.
    But i am developing a game plugin for a server my friend is starting. we have spent hours debugging and i added one or two lines of code in the game manager class and suddenly it breaks,
    i thought it may be a problem with the constructor of the gamemanager class, but the object would still be instantiated right? and my code looked fine infact i only did something i knew would work. and it wasnt that bit that i edited(BTW the variable that is null is gameinst which is an instance of GameManager class).
    This is my main class and before you ask, i know onEnable is being called because i put a debug there which i didn't need so i removed it .
    Code:java
    1.  
    2. package me.tommycake50.stuckinthemud;
    3.  
    4. import me.tommycake50.stuckinthemud.listeners.SignListener;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class StuckInTheMud extends JavaPlugin {
    14. public FileConfiguration config;
    15. public GameManager gameinst;
    16.  
    17. @Override
    18. public void onEnable(){
    19. saveDefaultConfig();
    20. gameinst = new GameManager(this);
    21. getServer().getPluginManager().registerEvents(gameinst, this);
    22. getServer().getPluginManager().registerEvents(new SignListener(this), this);
    23. getServer().setDefaultGameMode(GameMode.ADVENTURE);
    24. config = getConfig();
    25. }
    26.  
    27. @Override
    28. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    29. if(label.equalsIgnoreCase("stuck") && args.length == 0){
    30. displayHelp(sender);
    31. }else if(label.equalsIgnoreCase("stuck") && args.length >= 1 && args[0].equalsIgnoreCase("start") && sender.hasPermission("stuck.startgame")){
    32. if(getServer().getOnlinePlayers().length >= 3){
    33. if(gameinst != null && gameinst.isingame){
    34. sender.sendMessage("Game is already on!");
    35. }else{
    36. if(gameinst != null){
    37. gameinst.start();
    38. }else{
    39. sender.sendMessage("Game manager is null!"); //whenever i execute the command this pops up, so the game manager is surely null
    40. }
    41. }
    42. }else{
    43. sender.sendMessage(ChatColor.DARK_RED + "You need at least three players online to start a game of stuck!");
    44. }
    45. }
    46. return true;
    47. }
    48.  
    49. private void displayHelp(CommandSender sender) {
    50. sender.sendMessage(ChatColor.GREEN + "Usage: /stuck start");
    51. }
    52. }
    53.  

    So yeah, im truly baffled here so if you have any idea whats going on please try to help.
     
  2. Offline

    Tomskied

    Try setting your game manager to null to begin with.

    public GameManager gameinst = null;
     
  3. Offline

    Chiller

    No no no, you do not want this: gameinst =new GameManager(this);
    You always want to set the instance of your class in the constructor: public GameManager() {
    Do: gameinst = this;
     
  4. Offline

    Rocoty

    Tomskied Don't do that. First, it wouldn't change the outcome. Second, it would slow down performance slightly (insignificantly, but then again)

    Chiller What are you on about? You are not making any sense to me.

    tommycake50 Could you give me some stacktrace and GameManager fields and constructor?
     
  5. Offline

    tommycake50

    that is my main class.
    gameinst = this wouldn't
    that is my main class.
    gameinst = this wouldn't work because that is the instance of the plugin and the gamemanager class is separate entirely.
    your not making much sense at all.
     
  6. Offline

    Chiller

    tommycake50 Sorry, but does your GameManager class extend Listener
     
  7. Offline

    tommycake50

    Thats the odd thing about it, apparently when the plugin starts no error is thrown and checking if GameManager !=null stops nullpointerexceptions from occurring there.
    one of my old stacktraces before i put that in and put in the debug message was this:
    Show Spoiler

    16:41:20 [INFO] tommy2ndacc issued server command: /stuck start
    16:41:20 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stuc
    k' in plugin CentralStuckInTheMud v1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
    at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:964)
    at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    ava:882)
    at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :839)
    at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
    at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
    at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :118)
    at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
    at me.tommycake50.stuckinthemud.StuckInTheMud.onCommand(StuckInTheMud.ja
    va:35)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more


    Yes, yes it does.
    I would have seen if it didn't.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  8. Offline

    Tomskied

    Whats the constructor for your GameManager class?
     
  9. Offline

    tommycake50

    This:
    Code:java
    1.  
    2. public GameManager(StuckInTheMud inst){
    3. isingame = false;
    4. this.inst = inst;
    5. r = new Random();
    6. stuckees = new HashMap<String, Boolean>();
    7. stuckers = new ArrayList<String>();
    8. s = inst.getServer().getScoreboardManager();
    9. stuckers1 = s.getMainScoreboard().registerNewTeam("stuckers");
    10. stuckees1 = s.getMainScoreboard().registerNewTeam("stuckees");
    11. }
    12.  

    TBH a lot of other variables are initialized as and when is needed, this is all i really needed to put in the constructor.
     
  10. Offline

    Tomskied

    tommycake50 Have you tried adding debug in your GameManager constructor, also at the end of your onEnable method to check the GameManager instance is not null?
     
  11. Offline

    tommycake50

    I'll try both now.
    actually funnily enough i did the constructor debugging and it worked.
     
  12. Offline

    Tomskied

    What about the checks at the end of onEnable?
     
  13. Offline

    Chiller

    tommycake50 Try making a separate class for the commands and do getCommand("command").setExecutor(new CommandClass(gameinst))
     
  14. Offline

    tommycake50

    Oh this im sorry, its because of a lack of communication between me and the owner of the server.
    I have found the error now he gave me the onenable error.
    sorry to be a waste of time xD.
    it was because i was registering the team every time the plugin started so the constructor wasnt running through and causing all sorts of problems.
    now i just need to unregister the team onDisable or whatever.

    I had that b4 anyway i have found the error now.

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

Share This Page