NullPointerException on create Config

Discussion in 'Plugin Development' started by yohannlog, Nov 10, 2016.

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

    yohannlog

    Hello,

    I have a problem with my plugin. I create a file per player on connexion but:

    Code:
    [23:34:22 ERROR]: Could not pass event PlayerJoinEvent to Utilities v1.0.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.PlayerList.onPlayerJoin(PlayerList.java:333) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.PlayerList.a(PlayerList.java:159) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.LoginListener.b(LoginListener.java:144) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.LoginListener.E_(LoginListener.java:54) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.NetworkManager.a(NetworkManager.java:233) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.ServerConnection.c(ServerConnection.java:140) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:832) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:673) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:572) [spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_112]
    Caused by: java.lang.ExceptionInInitializerError
            at fr.yohannlog.utilities.Utilities.onPlayerJoin(Utilities.java:55) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_112]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            ... 14 more
    Caused by: java.lang.NullPointerException
            at fr.yohannlog.utilities.config.PlayerFile.<clinit>(PlayerFile.java:15) ~[?:?]
            at fr.yohannlog.utilities.Utilities.onPlayerJoin(Utilities.java:55) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_112]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.10.2.jar:git-Spigot-a9631d2-67b2424]
            ... 14 more
    

    Main class:

    Code:
    public class Utilities  extends JavaPlugin implements Listener{
    
        private static Utilities plugin;
        Logger log = Logger.getLogger("Utilities");
    
        //FILES
       // public static MyMenuConfig configFile;
       // public static YamlConfiguration config;
    
        @Override
        public void onEnable() {
            log.info("JFIUHFBZHFBRGREGHERE");
            getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @Override
        public void onDisable(){
    
        }
    
    
    
    
        public static Utilities getInstance(){
            return plugin;
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            Player player = e.getPlayer();
            Bukkit.broadcastMessage(player.getDisplayName());
            PlayerFile.initMapData(player);
        }
    }
    



    Player File:


    Code:
    public class PlayerFile {
        public static File mapsFolder = new File(Utilities.getInstance().getDataFolder() + "/players");
    
    
        public static void initMapData(Player p) {
            try {
                if (!mapsFolder.exists()) mapsFolder.mkdir();
    
                File mapFile = new File(mapsFolder, p.getUniqueId()+ ".yml");
                FileConfiguration mapConf = YamlConfiguration.loadConfiguration(mapFile);
                if (!mapFile.exists()) {
                    mapFile.createNewFile();
    
                    mapConf.addDefault("UUID", p.getUniqueId().toString());
                    mapConf.addDefault("Name", p.getDisplayName());
                    mapConf.addDefault("IP", p.getAddress());
                    mapConf.addDefault("Frozen", false);
                    mapConf.addDefault("Banned.ban", false);
                    mapConf.addDefault("Banned.reason", "");
                    mapConf.addDefault("Banned_info", null);
                    mapConf.addDefault("Muted", false);
                    mapConf.addDefault("Muted_info", null);
                  //  mapConf.addDefault("Balance",  EconomyManager.getBalance(p));
                    mapConf.addDefault("Gamemode", p.getGameMode().toString());
                    mapConf.addDefault("Vanish", false);
                    mapConf.addDefault("Health", p.getHealth() /2);
                    mapConf.addDefault("Feed", p.getFoodLevel());
                    mapConf.addDefault("Fly", false);
                    mapConf.addDefault("Level.amount", p.getLevel());
                    mapConf.addDefault("Level.lock", false);
                    mapConf.addDefault("Effects", p.getActivePotionEffects());
                    mapConf.addDefault("Position.x", p.getLocation().getX());
                    mapConf.addDefault("Position.y", p.getLocation().getY());
                    mapConf.addDefault("Position.z", p.getLocation().getZ());
                    mapConf.addDefault("Maintenance_accept", false);
                    mapConf.addDefault("Mention.activated", true);
                    mapConf.addDefault("Mention.text.color", ChatColor.WHITE.toString());
                    mapConf.addDefault("Mention.text.bold", false);
                    mapConf.addDefault("Mention.text.underline", false);
                    mapConf.addDefault("Mention.text.italic", false);
                    mapConf.addDefault("Mention.sound.activated", true);
                    mapConf.addDefault("Mention.sound.type", "BLOCK_GRASS_HIT");
                    mapConf.addDefault("Mention.sound.pitch", 1.0F);
                    //  mapConf.options().header("IKDEIFH");
                    mapConf.addDefault("Silent_join", false);
                    mapConf.addDefault("View_Damage", false);
                    mapConf.addDefault("Damage_view", false);
    
                    mapConf.save(mapFile);
                }
            } catch (Exception e) {
                System.err.println("An error occured while initializing Player (" + p + ")");
                System.err.println("Please report this error: " + e.getMessage());
                e.printStackTrace();
            }
        }
    
    
    }
    
    

    Yohann
     
  2. Offline

    PhantomUnicorns

    Utilities class? I think it is initializing mapsFolder wrong
     
  3. Offline

    Zombie_Striker

    Because this is static, it is trying to get the plugin before the plugin is even enabled. That means the instance would be null and would throw an NPE (which is what is happening).

    Only set the file variable AFTER the plugin has been enabled.

    BTW: Your "Utility" class should not be the main class. Your main class should control all the features for your plugin, not be a Utility for other classes. I would recommend changing the name/ idea for this class.
     
  4. Offline

    yohannlog

    It works by i have any text in the config ...

    Main:
    Code:
    public class Main extends JavaPlugin implements Listener{
    
    
        private static Main plugin;
        Logger log = Logger.getLogger("Utilities");
        private static PlayerFile pf;
    
    
        //FILES
       // public static MyMenuConfig configFile;
       // public static YamlConfiguration config;
    
        @Override
        public void onEnable() {
            plugin = this;
            log.info("JFIUHFBZHFBRGREGHERE");
            getPluginManager().registerEvents(this, this);
    
        }
        public static Main getInstance(){
            return plugin;
        }
    
        @Override
        public void onDisable(){
    
        }
    
    
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            Player player = e.getPlayer();
            Bukkit.broadcastMessage(player.getDisplayName());
            PlayerFile.initMapData(player);
        }
    }
    






    File:

    Code:
    package fr.yohannlog.utilities.config;
    
    import fr.yohannlog.utilities.Main;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    import java.io.File;
    
    /**
    * @author Yohannlog
    */
    public class PlayerFile {
        private static File mapsFolder = new File(Main.getInstance().getDataFolder() + "/players");
    
    
        public static void initMapData(Player p) {
            try {
                if (!mapsFolder.exists()) mapsFolder.mkdir();
    
                File mapFile = new File(mapsFolder, p.getUniqueId()+ ".yml");
                FileConfiguration mapConf = YamlConfiguration.loadConfiguration(mapFile);
                if (!mapFile.exists()) {
    
                    mapFile.createNewFile();
                    mapConf.addDefault("UUID", p.getUniqueId().toString());
                    mapConf.addDefault("Name", p.getDisplayName());
                    mapConf.addDefault("IP", p.getAddress());
                    mapConf.addDefault("Frozen", false);
                    mapConf.addDefault("Banned.ban", false);
                    mapConf.addDefault("Banned.reason", "");
                    mapConf.addDefault("Banned_info", null);
                    mapConf.addDefault("Muted", false);
                    mapConf.addDefault("Muted_info", null);
                  //  mapConf.addDefault("Balance",  EconomyManager.getBalance(p));
                    mapConf.addDefault("Gamemode", p.getGameMode().toString());
                    mapConf.addDefault("Vanish", false);
                    mapConf.addDefault("Health", p.getHealth() /2);
                    mapConf.addDefault("Feed", p.getFoodLevel());
                    mapConf.addDefault("Fly", false);
                    mapConf.addDefault("Level.amount", p.getLevel());
                    mapConf.addDefault("Level.lock", false);
                    mapConf.addDefault("Effects", p.getActivePotionEffects());
                    mapConf.addDefault("Position.x", p.getLocation().getX());
                    mapConf.addDefault("Position.y", p.getLocation().getY());
                    mapConf.addDefault("Position.z", p.getLocation().getZ());
                    mapConf.addDefault("Maintenance_accept", false);
                    mapConf.addDefault("Mention.activated", true);
                    mapConf.addDefault("Mention.text.color", ChatColor.WHITE.toString());
                    mapConf.addDefault("Mention.text.bold", false);
                    mapConf.addDefault("Mention.text.underline", false);
                    mapConf.addDefault("Mention.text.italic", false);
                    mapConf.addDefault("Mention.sound.activated", true);
                    mapConf.addDefault("Mention.sound.type", "BLOCK_GRASS_HIT");
                    mapConf.addDefault("Mention.sound.pitch", 1.0F);
                    //  mapConf.options().header("IKDEIFH");
                    mapConf.addDefault("Silent_join", false);
                    mapConf.addDefault("View_Damage", false);
                    mapConf.addDefault("Damage_view", false);
    
                    mapConf.save(mapFile);
                }
            } catch (Exception e) {
                System.err.println("An error occured while initializing Player (" + p + ")");
                System.err.println("Please report this error: " + e.getMessage());
                e.printStackTrace();
            }
        }
    
    
    }
    
    
     
    Last edited: Nov 10, 2016
  5. Offline

    Lolmewn

Thread Status:
Not open for further replies.

Share This Page