Solved Need help on reading a string list

Discussion in 'Spigot Plugin Development' started by dkajlw, Jul 4, 2018.

  1. Here is my config, and the code I am using to read. It keeps giving me a ZERO result for the string and integers.

    Code (Text):

    for (String key : config.getConfigurationSection("depots").getKeys(false)) {

                String[] data = key.split("-");

                Material m = Material.getMaterial(data[0].toUpperCase());

                Material v = Material.getMaterial(config.getString("depots." + key +".exchange").toUpperCase());

                ArrayList<String> item = new ArrayList<String>(getConfig().getStringList("depots." + key+".items"));

                ArrayList<Integer> id = new ArrayList<Integer>(getConfig().getIntegerList("depots." + key+".data"));

                ArrayList<Integer> chance = new ArrayList<Integer>(getConfig().getIntegerList("depots." + key+".chances"));

                ArrayList<Integer> quant = new ArrayList<Integer>(getConfig().getIntegerList("depots." + key+".quantity"));

                int z=0;

                for(Integer k:id)DropTable[l][z++][0]=k;

                System.out.print("[DepotProvider] " + z+" items loaded");

                z=0;

                for(String k:item)

                {

                Drop[l][z++]=k.toUpperCase();

                System.out.print("[DepotProvider] "+data[0]+" Items: "+k);

                }

                System.out.print("[DepotProvider]" + z);

                drops.put(l,z);

                z=0;

                for(Integer k:chance)DropTable[l][z++][1]=k;

                z=0;

                for(Integer k:quant)DropTable[l][z++][2]=k;

                list.put(m,l);

                exchange.put(m,v);

                l++;

            }
     
    Code (Text):

    depot-regen: 10
    depots:
      log:
        exchange: BEDROCK
        droptable:
          items:
          - STICK
          - WOODEN_PLANK
          - LOG
          chances:
          - 100
          - 80
          - 40
          quantity:
          - 2
          - 1
          - 1
          data:
          - 0
          - 1
          - 1
      stone:
        exchange: BEDROCK
        droptable:
          items:
          - STICK
          - WOODEN_PLANK
          - LOG
          chances:
          - 100
          - 80
          - 40
          quantity:
          - 2
          - 1
          - 1
          data:
          - 0
          - 1
          - 1
    minimum-light: 7
    maximum-heat: 100
    damage-per-second: 1

     
     
    #1 dkajlw, Jul 4, 2018
    Last edited: Jul 4, 2018
  2. You should really use [ code ] blocks so your shit is readable. No one can tell if you have a simple indentation issue in yaml or something else.
     
  3. Fix the indentation of the yaml.
     
  4. Yup, indentation issue. Your lists should line up with the keys. Move them two spaces to the left.
     
  5. #5 dkajlw, Jul 4, 2018
    Last edited: Jul 4, 2018
  6. Your full key is wrong.
    "depots." + key +".exchange" works because depots.log.exchange is correct
    "depots." + key+".items" doesn't work because depots.log.items doesn't exist. It is missing the droptable part
     
    • Agree Agree x 1
  7. Code (Text):


    [14:45:51 INFO]: [DepotProvider] Loading DepotProvider v0.01

    [14:45:51 INFO]: [DepotProvider] Enabling DepotProvider v0.01

    [14:45:51 INFO]: Directory was created successfully

    [14:45:51 INFO]: [DepotProvider] 0 items loaded

    [14:45:51 INFO]: [DepotProvider]0

    [14:45:51 ERROR]: Error occurred while enabling DepotProvider v0.01 (Is it up to date?)

    java.lang.NullPointerException
    at org.od_network.StarvePlugin.onEnable(StarvePlugin.java:216) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [Custom.jar:git-Spigot-db6de12-18fbb24
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.Bukkit.reload(Bukkit.java:535) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Custom.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
     
    Exact same error, at the same line. The reason for this error is that I am inputting null into the file, but the problem is that it shouldn't be, since I am reading from the list/
     
  8. @iPyronic Haha, can't belive I made a mistake like that! I will repair and tell the result :)


    Edit: I managed to finally read from the lines, thank you!

    So all the reading from lists work, but why does this trigger a nullpointer error?


    drops.put(l,z);

    Drops is a Map for Integer and Integer. L is a Integer, z is also one.
     
  9. Use configurationsection.getKeys to get the depot entries. From there on you can iterate over all the depots.
     
  10. If it's saying there's a NPE on drops.put(l,z);, wouldn't that mean drops hadn't been initialized yet? If I suspect something shouldn't be null in my code, but something is causing a NPE... I like to add debug messages just to confirm everything is as I think it is.
     
    • Agree Agree x 1
  11. Yes, an NPE means you called a field/method on a null reference (null.something()). So drops would be null on that line.
     
  12. Okay, I made it work. It was initialized, and I don't know what was wrong or how I messed up, but I fixed it, and all seems to be well :) Just having somme minor problems on the total chances.

    Okay.
    Code (Text):

    ) [Custom.jar:git-Spigot-db6de12-18fbb24]

    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Custom.jar:git-Spigot-db6de12-18fbb24]

    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]

    Caused by: java.lang.NullPointerException

    at org.od_network.StarvePlugin.giveItems(StarvePlugin.java:228) ~[?:?]

    at org.od_network.StarvePlugin.onBreak(StarvePlugin.java:248) ~[?:?]

    at sun.reflect.GeneratedMethodAccessor25.invoke(Unknown Source) ~[?:?]

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]

    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]

    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Custom.jar:git-Spigot-db6de12-18fbb24]

    ... 17 more

    >

     
    Important part of stack trace is here.
    The problem is, the two lines......

    That it says are triggering a nullpointer error arre both

    {


    It has no commandsd no notthing, just {.
     
  13. Uhhh, that doesn't sound right at all lol. Delete the jar, make sure there are no other jar versions with different names in the plugin folder still. Then re-export and test to try to get the appropriate line number. Maybe even add a little broadcast or println somewhere that you can test to make sure the newly exported jar is in-fact the one you're running.
     
    • Winner Winner x 1
  14. Ehh.... The issue persists. It just spams the console with errors, but all the features work. Thank you for your help! Should I upload my plugin as a resource when I am done?


    It is a little specific, but
    It contains

    Heat
    Thirst
    and a Depot plugin that lets you configure drop tables of whatever you want when mining a block, and turns the block into bedrock.
     
  15. I recommend fixing the errors before uploading the plugin if sharing is something you're interested in doing. I like the depot concept though. If it's well made, then other people will probably like it too.
     
    • I have found the errors. If the Material Name in the config is even slightly wrong(capital/not is fine), it makes the whole plugin go awry. Any idea on a fix? I also need to find a method to add /depot reload or something.
     
  16. Code (Java):
    String[] data = key.split("-");
    I'm wondering why no one has seen this. Shouldn't this show an error? Looping a string list should usually return a string (in your case: depots or stone) so splitting it seems to be useless and wrong.
     
  17. @Dark that has no part in my code, that is to read the material as log-2 and stuff to take care of the data properties.
     
  18. drives_a_ford

    Moderator

    Find the piece of code that's reading the name from the config. You're probably reading the material as a string with getString() and then using that in Material.getMaterial(String materialname). After doing that, check if the Material instance is null (if it is, there's been a problem in your config and I would suggest logging that as a warning).

    EDIT: There is little you can do to bypass typos as it is extremely difficult to get the "intent" behind a misspelled word.
     
  19. Strahan

    Benefactor

    It isn't a problem to split a String, but it's bad design in this example. The schema should be updated to have the delimited data as properties of the parent rather than gluing it to the parent.