Solved NullPointerException on getData();

Discussion in 'Spigot Plugin Development' started by darkshadow22mc, Jun 1, 2016.

Thread Status:
Not open for further replies.
  1. Hi!
    Basically, I have a stack trace that returns me the next error:​
    Code (Text):
    [01:24:03 WARN]: [MenuAPI] Task #163 for MenuAPI v1.2 generated an exception
            at perks.ChatColorMethods.resetColor( ~[?:?]
            at perks.ChatColorMethods$11.onClick( ~[?:?]
            at com.colonelhedgehog.menuapi.listeners.MenuActions$
    ava:63) ~[?:?]
    :71) ~[spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.mainThreadHea
    rtbeat( [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(
    09) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(
    16) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(
    34) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
    :537) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
    [CENTER]        at Source) [?:1.8.0_91]
    Reading the error I get two lines:
    Code (Text):
    list = (ArrayList<String>) getData().get("darkblue");
    Code (Text):
    The 1st line is inside the 2nd method. Here you have the getData();
    Code (Text):
    public static FileConfiguration getData() {
            if (dataFile == null) {
            return data;
        public static void saveData() {
            if (data == null || dataFile == null) {
            try {
            } catch (IOException ex) {
                Nexus.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + dataFile, ex);
    [CENTER]    }
    If you want a full version of the code:[/CENTER]
  2. Why in the world are you casting a config to a ArrayList?
  3. Because if not Eclipse warns me with the following message:
    Type mismatch: cannot convert from Object to ArrayList<String>

    And it suggests:
    Add cast to (ArrayList<String>)
    Change type of list to Object
  4. FileConfiguration has a getStringList() method. Use it
    • Like Like x 1
  5. No, here you've a little bit of more information:
    Code (Text):
    List<String> list = getData().getStringList("darkblue");
  6. ... So?
  7. It doesn't work.Returns the same error.
  8. Does darkblue exist in the config?
  9. Yes,
    Code (Text):
    darkblue: []
    darkgreen: []
    darkaqua: []
    darkred: []
    darkpurple: []
    gold: []
    blue: []
    aqua: []
    green: []
    red: []
    lightpurple: []
    yellow: []
    white: []
  10. Are you trying to reference a value inside of darkblue? Because at the moment it's empty
  11. Tried this:
    Code (Text):
    List<String> list;
                list = getData().getStringList("darkblue");
    Still not working :p
  12. Do a null check on the darkblue StringList too :p
  13. Tried this:
    Code (Text):
    List<String> list;
            if(!getData().getStringList("darkblue").isEmpty() || getData().getStringList("darkblue") != null){
                list = getData().getStringList("darkblue");
    Got same error!
  14. Learn basic java, NullPointers are extremely easy to fix.

    You cannot just magically cast things to other totally unrelated things.

    Code (Java):

    public static FileConfiguration getData()
        if (dataFile == null)
        return data; // Where does data even come from? Nowhere is it declared, or initialize, or course it is null, The entire method does nothing.
  15. Could you tell me a solution? Also is code that I got from
  16. Solution: Learn java instead of flailing like Don Quixote and copying code you do not really understand. That code has so many things wrong with it..
    Save your own time save our time :

    For fixing NPEs, just find out what object in the given line is null, by assigning each one to a variable and checking it for if( XX == null) { System.out.println("ow!"); }. Once you identify the null field, trace it back through your code and find out where it is (failing to be) initialized.
    • Optimistic Optimistic x 1
  17. Also I execute this onEnable()

    Code (Text):
    public static void reloadData() {
            if (dataFile == null) {
            dataFile = new File(Nexus.plugin.getDataFolder(), "data.yml");
            data = YamlConfiguration.loadConfiguration(dataFile);

            // Look for defaults in the jar
            Reader defConfigStream = null;
            try {
                defConfigStream = new InputStreamReader(Nexus.plugin.getResource("data.yml"), "UTF8");
            } catch (UnsupportedEncodingException e) {
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  18. That's the worst solution that you could tell me. Every developer has his own methods of developing a code. Mine is like this. Also, I'm Spanish and include Don Quijote as an example of this topic doesn't have any sense.

    Could someone give me help?
  19. First learn how to avoid static cling (unnecessary static dependencies, rather certain all the static in your config class is not necessary) and how not to repeat the same lines over and over (it took me ages to scroll through that paste).

    Then, rather than reloading if the file is null and returning data, reload if data is null.
  20. The case is that all of the permissions are and should be individual. I thought in to do a switch-case, but isn't the thing that I'm looking for. So; please, could you give me help? I was looking at .If the api is bad, then that's not really my problem.
Thread Status:
Not open for further replies.