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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:308) ~[spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.PlayerList.onPlayerJoin(PlayerList.java:343) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.PlayerList.a(PlayerList.java:163) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.LoginListener.b(LoginListener.java:150) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.LoginListener.Y_(LoginListener.java:54) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.NetworkManager.a(NetworkManager.java:231) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.ServerConnection.c(ServerConnection.java:120) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.MinecraftServer.w(MinecraftServer.java:984) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.DedicatedServer.w(DedicatedServer.java:411) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.MinecraftServer.v(MinecraftServer.java:819) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at net.minecraft.server.v1_13_R1.MinecraftServer.run(MinecraftServer.java:717) [spigot.jar:git-Spigot-fe3ab0d-162bda9]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
        at blueblazes13.LearnPlugin.Texter.onJoin(Texter.java:33) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304) ~[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.io.File;
    import java.util.ArrayList;

    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    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;
       
        @Override
        public void onEnable() {
            getCommand("spy").setExecutor(new commandspy());
            this.getServer().getPluginManager().registerEvents(new Texter(), this);
           
            if (!PlrDatas.exists()) {
                saveResource("plrdata.yml", false);
            }
        }
       
        @Override
        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;
       
       
        @EventHandler
        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);
            }
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
           
           
            Player plr = (Player) event.getPlayer();
            String text = main.PlrConfig.getString("plrs");
            plr.sendMessage(text);
        }
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            event.setCancelled(true);
            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.