Boolean NPE?

Discussion in 'Plugin Development' started by Dreeass, May 16, 2012.

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

    Dreeass

    So I got a main class called Main and within that Main class I only have 1 command, in that command I tell it when the first argument is equal to a certain string that it has to execute a method from another class. And I get an error everytime I use plugin. which refers to the main class.


    Code:
    public static Main plugin;
     
        public CLASSNAME(Main instance) {
            plugin = instance;
        }

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
    3. String[] args) {
    4.  
    5.  
    6. if(sender instanceof Player) {
    7. // PLAYER
    8. Player player = (Player)sender;
    9. if(args.length >= 1) {
    10. if(args[0].equalsIgnoreCase("") && player.hasPermission("game.start")) {
    11. Game_Start.Start();
    12. return true;
    13. }
    14. else {
    15. Do blablbabla..
    16. }
    17. }
    18. }
    19.  


    Code:java
    1. public class Game_Start {
    2.  
    3.  
    4. public static Main plugin;
    5.  
    6. public Game_Start(Main instance) {
    7. plugin = instance;
    8. }
    9.  
    10. public static boolean Start(Player player) {
    11. try {
    12. if(plugin.started == true) {
    13. plugin.MessageConvert(player, "Messages.Join.GameAlreadyStarted");
    14. return true;
    15. }
    16. plugin.started = true;
    17. String worldName = plugin.getConfig().getString("Config.World");
    18. if(worldName != null) {
    19. World world = Bukkit.getWorld(worldName);
    20. if(world != null) {
    21.  
    22. }
    23. else {
    24. // WORLD == NULL
    25.  
    26. }
    27. }
    28. else {
    29. // WORLDNAME == NULL
    30. }
    31. }
    32. player.sendMessage("NPE");
    33. }
    34. return false;
    35. }
    36. }
    37.  


    A constant NPE occurs when executing the class that has to handle the command with the given arg.
     
  2. Offline

    CorrieKay

    your constructor is never used.

    plugin = instance;

    is never executed, therefore plugin is always null.
     
  3. Offline

    Dreeass

    I do use it, the code I gave is only the constructor. When I type plugin.loading (which is a boolean) it gives an NPE.
    Edit: When typing the beginning plugin. I already get all of the methods, variables etc. from the class.
     
  4. Offline

    r0306

    Dreeass
    Try this:
    Code:
    private Main plugin;
     
        public CLASSNAME(Main plugin) {
            this.plugin = plugin;
        }
     
    
     
  5. Offline

    Dreeass

    Doesn't work and that's useless, it's just referring to itself when it's null..
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    I think you will need to show more code....
     
  7. Offline

    Dreeass

    I added some code, is there anything else you need? The code I use to execute the command is just the regular onCommand... there shouldn't be the problem.
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    Don't use static methods, your problem will go away. Also due to the fact that you invoke a static method which requires a field to be instantiated upon construction of a object Game_start
     
  9. The static method is not the problem, the problem is you don't understand them.

    When you call new Class() it calls the constructor, when you call a static method it DOESN'T call the constructor (would be pointless to do that), so your constructor is not called and the plugin is NULL, as CorrieKay already said.

    The solution here would be to add a "new Game_Start(this);" in your onEnable(), altough it's not the best way to do what you're doing.
     
    Dreeass likes this.
  10. Offline

    Dreeass

    Thanks, I got into the constructor a bit more via tutorials and also the static and non-static. I think I get it now, ofcourse not as good as you do but you put me on the right track.
     
  11. Offline

    CorrieKay

    constructors are only called when an object is being "constructed" or created/initialized/whatever-you-call-it. :3
     
  12. Offline

    Dreeass

    Yes I get it, because it just got created it didn't have a value yet. Another thing I learned today!
    Edit: Question if you can answer: What string for a file is it when I get the folder world in the main directory of the server?
     
Thread Status:
Not open for further replies.

Share This Page