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
    java.lang.NullPointerException
            at perks.ChatColorMethods.resetColor(ChatColorMethods.java:709) ~[?:?]
            at perks.ChatColorMethods$11.onClick(ChatColorMethods.java:455) ~[?:?]
            at com.colonelhedgehog.menuapi.listeners.MenuActions$1.run(MenuActions.j
    ava:63) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:7
    09) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    34) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :537) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
    [CENTER]        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Reading the error I get two lines:
    1.-
    Code (Text):
    list = (ArrayList<String>) getData().get("darkblue");
    2.-
    Code (Text):
    resetColor(p);
    The 1st line is inside the 2nd method. Here you have the getData();
    [/CENTER]
    Code (Text):
    public static FileConfiguration getData() {
            if (dataFile == null) {
                reloadData();
            }
            return data;
        }
        public static void saveData() {
            if (data == null || dataFile == null) {
                return;
            }
            try {
                getData().save(dataFile);
            } 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:
    http://pastebin.com/QTLnjzJR[/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;
            if(!getData().getStringList("darkblue").isEmpty()){
                list = getData().getStringList("darkblue");
                list.remove(p.getName());
            }
    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");
                list.remove(p.getName());
            }
    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)
        {
            reloadData();
        }
        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 http://wiki.bukkit.org/Configuration_API_Reference
     
  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 : https://www.spigotmc.org/wiki/how-to-learn-java-programming/

    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) {
                e.printStackTrace();
            }
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                data.setDefaults(defConfig);
            }
        }
     
  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 http://wiki.bukkit.org/Configuration_API_Reference .If the api is bad, then that's not really my problem.
     
Thread Status:
Not open for further replies.