Solved Can't read from .yml file

Discussion in 'Spigot Plugin Development' started by blueblazes13, Mar 10, 2019.

Thread Status:
Not open for further replies.
  1. Hello.
    I'm trying to read from an .yml file, but when I try to read from it, it gives this error:

    Code (Text):
    [19:34:43 ERROR]: Could not pass event PlayerJoinEvent to LearnPlugin v1.0
    org.bukkit.event.EventException: null
        at$1.execute( ~[spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.PlayerList.onPlayerJoin( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.PlayerList.a( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.LoginListener.b( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.LoginListener.Y_( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.NetworkManager.a( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.ServerConnection.c( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.MinecraftServer.w( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.DedicatedServer.w( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.MinecraftServer.v( [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
        at blueblazes13.LearnPlugin.Texter.onJoin( ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
        at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_131]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_131]
        at java.lang.reflect.Method.invoke( ~[?:1.8.0_131]
        at$1.execute( ~[spigot.jar:git-Spigot-fe3ab0d-162bda9]
        ... 14 more
    I've watched many tutorials about this, but I just can't figure out why it gives this error.

    Here is my main class:

    Code (Java):
    package blueblazes13.LearnPlugin;

    import java.util.ArrayList;

    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class Main extends JavaPlugin {

        static ArrayList<String> Admins = new ArrayList<String>();
        static String NoPlayer = ChatColor.AQUA + "You must be a player to do that!";
        static String NotEnoughArgs = ChatColor.AQUA + "Not enough arguments!";

        File PlrDatas = new File(getDataFolder(), "plrdata.yml");
        FileConfiguration PlrConfig = YamlConfiguration.loadConfiguration(PlrDatas);
        String testers;
        public void onEnable() {
            getCommand("spy").setExecutor(new commandspy());
            this.getServer().getPluginManager().registerEvents(new Texter(), this);
            if (!PlrDatas.exists()) {
                saveResource("plrdata.yml", false);
        public void onDisable() {
        public File GetPlayerData() {
            return PlrDatas;
        public FileConfiguration GetPlayerConfig() {
            return PlrConfig;
    My listener class:

    Code (Java):
    package blueblazes13.LearnPlugin;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.player.PlayerJoinEvent;

    public class Texter implements Listener {
        private Main main;
        public void onCmd(PlayerCommandPreprocessEvent event) {
            Player plr = (Player) event.getPlayer();
            String cmd = event.getMessage();
            for (int i = 0; i < Main.Admins.size(); i++) {
                Player adm = (Player) Bukkit.getPlayer(Main.Admins.get(i));
                adm.sendMessage("" + ChatColor.GRAY + ChatColor.ITALIC + plr.getName() + ": " + cmd);
        public void onJoin(PlayerJoinEvent event) {
            Player plr = (Player) event.getPlayer();
            String text = main.PlrConfig.getString("plrs");
        public void onChat(AsyncPlayerChatEvent event) {
            Player plr = (Player) event.getPlayer();
            plr.sendMessage("" + ChatColor.DARK_PURPLE + event.getMessage());
    And my .yml file:

    Code (YAML):
    plrs: 'hi there!'
    I'm sorry if it's something stupid.
  2. In your Texter class you're just defining a variable of the Type Main which has the default value null if you don't assign it to anything.
    And because you never initialize it with anything you variable stays null and then you're trying to access a field of null which of course doesn't work and throws a NullPointerException.

    So what you'll need now is to access an instance of your Main class in your other class. If you don't know how to do this then I highly recommend to stop making plugins and just learning plain java until you're familiar with it.
  3. Also learn the naming conventions and how to use constants.
  4. Omg, I'm sorry! I was too busy with the code for the yml file so I forgot to write it down and never looked back to it. And sorry for the messy code. I know it looks terrible. I was just messing with it.
    I'm feeling so stupid now..
    I've changed it an now it works, thank you!
Thread Status:
Not open for further replies.