ExceptionInInitializerError - Custom files.

Discussion in 'Spigot Plugin Development' started by StefanoGuida, May 11, 2017.

  1. ERROR
    Code (Text):
    [19:22:06] [Server thread/ERROR]: Error occurred while enabling RegionsWar v1.0 (Is it up to date?)
    java.lang.ExceptionInInitializerError
        at me.stefanoguida.regionswar.RegionsWar.loadFiles(RegionsWar.java:62) ~[?:?]
        at me.stefanoguida.regionswar.RegionsWar.onEnable(RegionsWar.java:109) ~[?:?]
        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 net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_112]
    Caused by: java.lang.NullPointerException
        at me.stefanoguida.regionswar.api.utils.RUtils.<clinit>(RUtils.java:41) ~[?:?]
        ... 13 more
     
    This is the custom file class.
    Code (Text):
    package me.stefanoguida.regionswar.api.objects;

    import me.stefanoguida.regionswar.api.utils.RUtils;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import java.io.File;
    import java.io.IOException;

    /**
    * RFile.
    *
    * @author StefanoGuida.
    */
    public class RFile extends RUtils {

        /**
         * Variables.
         */

        /* File. */
        private File file;

        /* FileConfiguration. */
        private FileConfiguration fileConfiguration;

        /**
         * Constructor of the class.
         *
         * @param path File's path.
         */
        public RFile(String path) {

            file = new File(instance.getDataFolder(), path);
            fileConfiguration = YamlConfiguration.loadConfiguration(file);

        }

        /**
         * Methods.
         */

        /* Getter of File. */
        public File getFile() {
            return file;
        }

        /* Getter of FileConfiguration. */
        public FileConfiguration getFileConfiguration() {
            return fileConfiguration;
        }

        /* Create file. */
        public void create() {

            try {

                if (file.exists()) {
                    instance.getLogger().info("File " + file.getName() + " already exists.");
                } else if (file.createNewFile()) {
                    instance.getLogger().info("File " + file.getName() + " has been created.");
                } else {
                    instance.getLogger().info("Failed to create file " + file.getName() + ".");
                }

            } catch (IOException exception) {
                exception.printStackTrace();
            }

        }

        /* Check if file exists. */
        public boolean exists() {
            return file.exists();
        }

        /* Save file. */
        public void save() {

            try {
                fileConfiguration.save(file);
            } catch (IOException exception) {
                exception.printStackTrace();
            }

        }

        /* Delete file. */
        public void delete() {
            file.delete();
        }
    Code (Text):

    /**
    * RegionsWar.
    *
    * @author StefanoGuida.
    */
    public class RegionsWar extends JavaPlugin {

        /**
         * Variables.
         */

        /* Plugin's instance. */
        private static RegionsWar instance;

        /* WorldEdit's instance. */
        private static WorldEditPlugin worldEdit;

        /* PermissionsEx's instance. */
        private static PermissionsEx permissionsEx;

        /* Plugin's prefix. */
        private static String prefix;

        /* Plugin's files. */
        public File configFile;
        public RFile lobbys;
        public RFile regions;
        public RFile assignments;

        /* Plugin's countdowns. */
        //TODO

        /**
         * Loader.
         */

        /* Load files. */
        private void loadFiles() {

            configFile = new File(getDataFolder(), "config.yml");

            if (!configFile.exists()) {
                saveDefaultConfig();
                getLogger().info("File " + configFile.getName() + " has been created.");
            }

            lobbys = new RFile("lobbys.yml");
            regions = new RFile("regions.yml");
            assignments = new RFile("assignments.yml");

            if (!lobbys.exists()) lobbys.create();
            if (!regions.exists()) regions.create();
            if (!assignments.exists()) assignments.create();


        }

        /* Register commands. */
        private void registerCommands() {

            getCommand("setprelobby").setExecutor(new SetPrelobbyCmd());
            getCommand("setlobby").setExecutor(new SetLobbyCmd());

        }

        /* Register listeners. */
        private void registerListeners() {

        }

        /**
         * OnEnable.
         */
        @Override
        public void onEnable() {

            instance = this;
            worldEdit = (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit");
            permissionsEx = (PermissionsEx) Bukkit.getPluginManager().getPlugin("PermissionsEx");

            if (worldEdit == null) {
                getLogger().severe("WorldEdit not found. Plugin disabled.");
                return;
            }

            if (permissionsEx == null) {
                getLogger().severe("PermissionsEx not found. Plugin disabled.");
                return;
            }

            prefix = getConfig().getString("Prefix") + " &8» &f";

            getLogger().info("Loading files...");
            loadFiles();
            getLogger().info("Files loaded.");
            registerCommands();
            registerListeners();
            getLogger().info("Plugin enabled " + getDescription().getVersion());

        }

        /**
         * OnDisable.
         */
        @Override
        public void onDisable() {

            getLogger().info("Plugin disabled " + getDescription().getVersion());

        }

        /**
         * Methods.
         */

        /* Getter of plugin's instance. */
        public static RegionsWar getInstance() {
            return instance;
        }

        /* Getter of WorldEdit's instance. */
        public static WorldEditPlugin getWorldEdit() {
            return worldEdit;
        }

        /* Getter of plugin's prefix. */
        public static String getPrefix() {
            return prefix;
        }

        /* Get RPlayer form Player. */
        public static RPlayer getPlayer(Player player) {
            RPlayer rplayer = new RPlayer(player);
            return rplayer;
        }
    Code (Text):
    public class RUtils {

        /**
         * Variables.
         */

        /* Plugin's instance. */
        protected static RegionsWar instance = RegionsWar.getInstance();

        /* WorldEdit's instance. */
        protected static WorldEditPlugin worldEdit = RegionsWar.getWorldEdit();

        /* Plugin's prefix. */
        protected static String prefix = RegionsWar.getPrefix();

        /* Plugin's files. */

        protected static File configFile = instance.configFile;
        protected static FileConfiguration config = instance.getConfig();

        protected static RFile lobbys = instance.lobbys;
        protected static File lobbysFile = lobbys.getFile();
        protected static FileConfiguration lobbysFC = lobbys.getFileConfiguration();

        protected static RFile regions = instance.regions;
        protected static File regionsFile = regions.getFile();
        protected static FileConfiguration regionsFC = regions.getFileConfiguration();

        protected static RFile assignments = instance.assignments;
        protected static File assignmentsFile = assignments.getFile();
        protected static FileConfiguration assignmentsFC = assignments.getFileConfiguration();
     
  2. Basically your issue is obviously in your "RUtils" Class, don't assign the classes directly, initialize them in a constructor.