Solved Error getting the main class

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, May 18, 2016.

Thread Status:
Not open for further replies.
  1. 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;
    But i keep getting an NPE

    Code (stacktrace (Unknown Language)):

    [11:13:18 ERROR]: Error occurred while enabling FlagPvP v1.0 (Is it up to date?)
            at FlagPvP.Commands.block.<init>( ~[?:?]
            at FlagPvP.Main.onEnable( ~[?:?]
            at ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.Bukkit.reload( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.defaults.ReloadCommand.execute( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at Source) [?:1.8.0_73]
  2. The instance you're passing is probably null.
  3. 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.
  4. But am i getting the instance wrong? As far as I know i am not
  5. Sorry we posted at the same time XD
  6. Can you show us your main class?
  7. I just tried what Husky said and i got a java.lang.IllegalArgumentException: Plugin already initialized!

    Code (Java):

    package FlagPvP;


    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.PluginManager;

    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);
        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(){
            try {
            } catch (IOException e) {

           private void createFiles() {
               configs = new File(getDataFolder(), "config.yml");
               data = new File(getDataFolder(), "Data.yml");

               if (!configs.exists()) {
                   saveResource("config.yml", false);
               if (!data.exists()) {
                   saveResource("Data.yml", false);

               FileConfiguration config1 = new YamlConfiguration();
               FileConfiguration data1 = new YamlConfiguration();
               try {
               } catch (Exception e) {
  8. Why? Just use Singleton...

    Code (Text):
    public class Example extends JavaPlugin {

    private static Example instance;

    public void onEnable() { instance = this; }

    public static Example getInstance() {
        return instance;

    Use it
    Code (Text):
    Example.getInstance(); // All your classes!

    Enjoy! :D
  9. You aren't passing 'this'.
  10. More... You are not respecting the naming conventions.
  11. I don't see the method I showed in your main class.
  12. What?
  13. Your class' constructor does not correspond to the one in the OP.
    Please, you win one point on the multi-threading but Spigot are one thread. :)
  14. 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.
  15. 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.
  16. Show us your new code?
  17. 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.
  18. Orly? What about Chat, Chunks, Database queries or Bukkit's async scheduler?
  19. True, true, true. But the Singleton is simple to use! And normally no cause lag or errors (only if you go in the depth).
Thread Status:
Not open for further replies.