Solved Converting a List to a Set

Discussion in 'Spigot Plugin Development' started by DevRice, Jun 2, 2017.

  1. Hello! I am trying to convert a list from a YAML file and convert that into a Set and then take that Set and place it into a HashMap<String, Set<String>>. It seems just fine when I convert it, the error is popping up when I try putting it into the Map.

    Code (Text):
    for(String name : inventoryNames){
                    Set<String> membersRef = new HashSet<>(inventoriesRef.getStringList("inventories." + name + ".members"));
                    Set<String> ownersRef = new HashSet<>(inventoriesRef.getStringList("inventories." + name + ".owners"));
                    Set<String> officersRef = new HashSet<>(inventoriesRef.getStringList("inventories." + name + ".officers"));
                    String inventoryRef = inventoriesRef.getString("inventories." + name + ".inventory");

                    if(membersRef.isEmpty())
                        members = new HashMap<>();
                    members.put(name, membersRef); //ERROR HERE
                    if(ownersRef.isEmpty())
                        owners = new HashMap<>();
                    owners.put(name, new HashSet<>(ownersRef));
                    if(officersRef.isEmpty())
                        officers = new HashMap<>();
                    officers.put(name, new HashSet<>(officersRef));
                    if(inventoryRef == null)
                        inventories = new HashMap<>();
                    inventories.put(name, inventoryRef);
                }
    Error message:

    Code (Text):
    java.lang.NullPointerException
        at io.github.collabstorage.CollabStorage.onEnable(CollabStorage.java:54) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:361) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:323) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:729) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.Bukkit.reload(Bukkit.java:549) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:629) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:615) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:397) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:361) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:650) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_111]
    Any help is greatly appreciated! Thank you.
     
    #1 DevRice, Jun 2, 2017
    Last edited: Jun 2, 2017
  2. what's the error?
     
  3. Oops, I'll edit my original post
     
  4. I didn't read the whole code but you may do this to convert easily
    Code (Java):
    List<String> list = new ArrayList<String>();
    Set<String> set = new Set<String>(list);
     
  5. debug name and membersref
     
  6. I did that, the issue is when I try to put that new set into a hashmap.
     
  7. also check if inventoryNames is not null before having loop on it.
     
  8. I have that, I just didn't post all my code.
     
  9. how about debugging? if the list is created successfully and if the sets are not null.
     
  10. If you mean check if they are not null, I did, they are perfectly fine. I'm not too sure why it's not working..
     
  11. then members is null (the map itself)
     
  12. I mean debug
    Code (Java):
    List<String> list = new ArrayList<>();
    System.out.print("created successfully");
     
  13. Oh god. I'm such an idiot.. I set the map as null... Thank you hahaha(nervous laugh)
     
  14. the same you did at inventoryRef and also mark the thread as solved.