When trying to get/set with the YAML Custom Configuration it throws an NPE. Error: Code (Text): Caused by: java.lang.NullPointerException at sharked.io.userstyles.UserStyles.onJoin(UserStyles.java:64) ~[?:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[server.jar:git-Spigot-db6de12-18fbb24] ... 14 more Main Class: Code (Java): private DataFile users; private DataFile config; private HashMap<Player, ChatColor> style = new HashMap<>(); @Override public void onEnable() { Bukkit.getPluginManager().registerEvents(this, this); DataFile users = new DataFile(this, "users", true); DataFile config = new DataFile(this, "config", true); } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { Player p = (Player) sender; if(command.getName().equalsIgnoreCase("style")){ users.getConfig().set("users." + p.getUniqueId() + ".style", "bold"); // Error Here users.saveConfig(); p.sendMessage("Style Set"); return true; } return false; } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onChat(AsyncPlayerChatEvent e){ Player p = (Player) e.getPlayer(); if(users.getConfig().getConfigurationSection("users").getKeys(false).contains(p.getUniqueId())){ // Error Here switch (users.getConfig().get("users." + p.getUniqueId() + ".style").toString().toLowerCase()){ // Error Here case ("bold"): style.put(p, ChatColor.BOLD); } e.setFormat(e.getFormat().replaceFirst(p.getName(), style.get(p) + p.getName())); } } @EventHandler public void onJoin(PlayerJoinEvent e){ Player p = (Player) e.getPlayer(); if(!users.getConfig().getConfigurationSection("users").getKeys(false).contains(p.getUniqueId())){ users.getConfig().set("users", p.getUniqueId()); // Error Here users.saveConfig(); } } DataFile Class: Code (Java): public class DataFile { private JavaPlugin plugin; private FileConfiguration configuration; private boolean hasDefault; private File file; private String fileName; public DataFile(JavaPlugin plugin, String fileName, boolean hasDefault) { this.plugin = plugin; this.hasDefault = hasDefault; this.fileName = fileName; file = new File(plugin.getDataFolder() + File.separator + fileName + ".yml"); reload(); } public void reload() { if (!file.exists()) { plugin.getDataFolder().mkdirs(); try { if (hasDefault) { plugin.saveResource(fileName + ".yml", false); } else { file.createNewFile(); } } catch (IOException ex) { ex.printStackTrace(); } } loadConfig(); } public void loadConfig() { configuration = new YamlConfiguration(); try { configuration.loadFromString(Files.toString(file, Charset.forName("UTF-8"))); } catch (IOException | InvalidConfigurationException ex) { ex.printStackTrace(); } } public FileConfiguration getConfig() { return configuration; } public String getString(String key) { return configuration.getString(key); } public double getDouble(String key) { return configuration.getDouble(key); } public void saveConfig() { try { configuration.save(file); } catch (IOException ex) { ex.printStackTrace(); } } public File getFile() { return file; } }
You haven't assigned values to the instance variables, but you declare and initialize local variables with the exact same time. Use this code instead: Code (Java): this.users = new DataFile(this, "users", true); this.config = new DataFile(this, "config", true);
Well, then you most likely miss some more null checks in your code. For example, FileConfiguration#getConfigurationSection and FileConfiguration#get return null if no value could be found. However, the exception should have changed slightly (Line number in which the exception is thrown).