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 @Overridepublic boolean onCommand(CommandSender sender, Command cmd, String commandLabel,String[] args) { if(sender instanceof Player) {// PLAYERPlayer player = (Player)sender;if(args.length >= 1) {if(args[0].equalsIgnoreCase("") && player.hasPermission("game.start")) {Game_Start.Start();return true;}else {Do blablbabla..}}} Code:java public class Game_Start { public static Main plugin; public Game_Start(Main instance) {plugin = instance;} public static boolean Start(Player player) {try {if(plugin.started == true) {plugin.MessageConvert(player, "Messages.Join.GameAlreadyStarted");return true;}plugin.started = true;String worldName = plugin.getConfig().getString("Config.World");if(worldName != null) {World world = Bukkit.getWorld(worldName);if(world != null) { }else {// WORLD == NULL }}else {// WORLDNAME == NULL}}catch (NullPointerException n) {player.sendMessage("NPE");}return false;}} A constant NPE occurs when executing the class that has to handle the command with the given arg.
your constructor is never used. plugin = instance; is never executed, therefore plugin is always null.
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.
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.
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
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.
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.
constructors are only called when an object is being "constructed" or created/initialized/whatever-you-call-it. :3
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?