How do i get the main class to use in another class? I am trying Code (Java): Main plugin; public block(Main plugin){ this.plugin = plugin; System.out.println(plugin); } But i keep getting an NPE StackTrace: Code (stacktrace (Unknown Language)): [11:13:18 ERROR]: Error occurred while enabling FlagPvP v1.0 (Is it up to date?) java.lang.NullPointerException at FlagPvP.Commands.block.<init>(block.java:20) ~[?:?] at FlagPvP.Main.onEnable(Main.java:24) ~[?:?] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24] at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
You can use this method instead, I like is better because it gives the instance of your main that is already created automatically on server start: Code (Text): public static Main getMain() { return Main.getPlugin(Main.class); } Then you can just use Main.getMain() to get your main instance.
I just tried what Husky said and i got a java.lang.IllegalArgumentException: Plugin already initialized! Main Code (Java): package FlagPvP; import java.io.File; import java.io.IOException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import FlagPvP.Commands.Locb; import FlagPvP.Commands.TPloc; import FlagPvP.Commands.block; import FlagPvP.Commands.blocktp; import FlagPvP.Commands.gamefinished; import FlagPvP.Commands.setLoc; import FlagPvP.Listeners.BlockBreak; import FlagPvP.Listeners.BlockBurn; import FlagPvP.Listeners.StartCount; public class Main extends JavaPlugin{ public void onEnable(){ getCommand("setloc").setExecutor(new setLoc(this)); getCommand("block").setExecutor(new block()); getCommand("Locb").setExecutor(new Locb(this)); getCommand("blocktp").setExecutor(new blocktp(this)); getCommand("tploc").setExecutor(new TPloc(this)); getCommand("finishedLoc").setExecutor(new gamefinished(this)); new BlockBreak(this); new BlockBurn(this); new block(); new Locb(this); new blocktp(this); new TPloc(this); new StartCount(this); new gamefinished(this); createFiles(); } public void registerEvents(){ PluginManager pm = getServer().getPluginManager(); pm.registerEvents(new BlockBreak(this), this); pm.registerEvents(new BlockBurn(this), this); } FileConfiguration config1; FileConfiguration data1; File configs; File data; public FileConfiguration getData() { return this.data1; } public FileConfiguration getConfigs() { return this.config1; } public void saveConfigs(){ createFiles(); try { data1.save(data); config1.save(configs); } catch (IOException e) { e.printStackTrace(); } } private void createFiles() { configs = new File(getDataFolder(), "config.yml"); data = new File(getDataFolder(), "Data.yml"); if (!configs.exists()) { configs.getParentFile().mkdirs(); saveResource("config.yml", false); } if (!data.exists()) { data.getParentFile().mkdirs(); saveResource("Data.yml", false); } FileConfiguration config1 = new YamlConfiguration(); FileConfiguration data1 = new YamlConfiguration(); try { config1.load(configs); data1.load(data); } catch (Exception e) { e.printStackTrace(); } } }
Why? Just use Singleton... Code (Text): public class Example extends JavaPlugin { private static Example instance; @Override public void onEnable() { instance = this; } public static Example getInstance() { return instance; } } Use it Code (Text): Example.getInstance(); // All your classes! Enjoy!
When I wanna get the main class, for getting config, I just do this Code (Java): protected <main class> plugin; In the class I want to grab something from.
Just tried and got the same error And I removed Husky's code that way i could get back to what it originally was as it did not work.
The code I showed should work. I use it in all my plugins. Make sure you are putting in your main class not your other classes.
True, true, true. But the Singleton is simple to use! And normally no cause lag or errors (only if you go in the depth).