Thanos Paravantis
Thanos Paravantis

Reputation: 7693

Retrieving the correct file and data for a player

I am having some difficulties retrieving data from files. To start with, here is my main method for file creation, which will be triggered (later) when a player logs in to the server.

// SettingsManager.java

private File file;
private FileConfiguration config;

// ...

public void createFile(String fileName) {
    file = new File(UnderCovern.getPlugin().getDataFolder(), fileName + ".yml");

    if ( !file.exists()) {
        try {
            file.createNewFile();
        } catch (Exception e) {
            UtilLogger.exception(e, "[SettingsManager] Could not create new file.");
        }
    }

    config = YamlConfiguration.loadConfiguration(file);
}

The file is being created normally for each user - in another class -, with their name as the filename as seen here.

SettingsManager.get().createFile(player.getName());

However, I do not know exactly how I can retrieve the correct .yml file for a user, and retrieve data from it. This code block throws me a NullPointerException.

public Rank getRank() {
    if (SettingsManager.get().getConfig().get(player.getName()) != null)
        return (Rank) SettingsManager.get().getConfig().get(player.getName() + ".Rank");

    return null;
}

Please also note that I am using the Bukkit framework.

Upvotes: 0

Views: 56

Answers (1)

bramhaag
bramhaag

Reputation: 135

Your method:

public Rank getRank() {
    if (SettingsManager.get().getConfig().get(player.getName()) != null)
        return (Rank) SettingsManager.get().getConfig().get(player.getName() + ".Rank");

    return null;
}

Returns null if if (SettingsManager.get().getConfig().get(player.getName()) != null) is equal to null. So, the file probably isn't created, or you try to get a not-existing file. Can I maybe see the .getConfig() method?

Upvotes: 1

Related Questions