Solved Configuration section

Discussion in 'Spigot Plugin Development' started by swendel, Jan 31, 2020.

  1. My configuration section sometimes returns the string given, but then sometimes it just doesn't work and returns a nullpointerexception for no reason
    This is the line:
    Code (Java):
            String name = (String) ItemConfig.get().getConfigurationSection(classType + "Weapons." + tier + "." + rarity).getKeys(false).toArray()[new Random().nextInt(ItemConfig.get().getConfigurationSection(classType + "Weapons." + tier + "." + rarity).getKeys(false).size())];
     
    I need help finding how to fix it, I checked if it was null and everything but it isn't and returns the string even with the nullpointer in console
    If you need more info please tell me
     
  2. FrostedSnowman

    Resource Staff

    Paste the configuration and the entirety of the code that is relevant to the one you provided.
     
  3. Choco

    Moderator

    And also, for the love of God, please make a variable and either create constants for your Randoms or use ThreadLocalRandom
    Code (Java):
    ConfigurationSection section = ItemConfig.get().getConfigurationSection(classType + "Weapons." + tier + "." + rarity);
    Set<String> sectionKeys = section.getKeys(false);
    ConfigurationSection randomChild = Iterables.get(sectionKeys, ThreadLocalRandom.current().nextInt(sectionKeys.size()));
     
  4. Why do you want to make constants for Randoms? Is it a performance thing?
     
  5. FrostedSnowman

    Resource Staff

    If multiple threads use the same instance of a Random, the same seed is shared by multiple threads. This can possibly lead to contention between multiple threads and so to performance degradation. The solution to this is ThreadLocalRandom, additionally, it saves you the trouble of having to cache your own Random instance.

    The creation of Random certainly has a higher creation cost than other objects. This can become significant when constantly creating them in heavy intervals.
     
    • Like Like x 1
    • Agree Agree x 1
  6. Choco

    Moderator

    Correct. A correction on my initial statement though, "constants" may not necessarily be the correct term here. Have them created as little as possible. For instance, the World class (from Minecraft) uses just one single instance of Random. Whether or not new instances of random are created for related classes is irrelevant but if random nature is to do with the world, that single instance is passed down through its various methods.

    Frequently in Forge modding you'll notice people use entity.world.rand to access an instance of Random rather than creating their own.
     
    • Like Like x 1
  7. Strahan

    Benefactor

    Holy compound statements Batman! lol

    You really should avoid compound statements that deep. When shit happens and you get NPE on that line, it could be so many different things causing it, makes it annoying to debug. Plus it's just way cleaner. Yea, gonna take more lines, but it is far, far easier to read down the road.
     
  8. Thanks for the info about the random, I fixed that
    Code (Java):
    ConfigurationSection weapons = ItemConfig.get().getConfigurationSection(classType + "Weapons." + tier + "." + rarity);
            String name = (String) weapons.getKeys(false).toArray()[ThreadLocalRandom.current().nextInt(weapons.getKeys(false).size())];
    Here's my config, nothing's wrong with it
    Code (YAML):
    KnightWeapons:
      1
    :
        Common
    :
          Wood_Sword
    :
            DamageMin
    : 1-3
            DamageHigh
    : 3-5
            CriticalChance
    : 1-5
        Rare
    :
     
      2
    :
      3
    :
      4
    :
     5:
    I don't understand why but when it randomly returns this it always returns an NPE but also returns the name before it in console. Any ideas? Thx
     
    #8 swendel, Jan 31, 2020
    Last edited: Feb 1, 2020
  9. Anyone have any ideas?
     
  10. Strahan

    Benefactor

    Well, the two things that could be null are the ItemConfig and the weapons ConfigurationSection. Put some debugging in to determine which is null.
     
  11. Weapons is null even though the file isn't empty and it's loaded in correctly in onEnable. Is there any way to ensure it isn't null?
     
  12. if (weapons != null)
     
  13. Yea, but is there a way to make it so it always works and doesn't just return null sometimes?
     
  14. config.getX(path, default value)
     
  15. I need the whole configuration section, and it'll still return null like that
     
  16. You can also create a hardcoded default configuration section and pass that as default value.
     
  17. Yea I tried that before, if you look above at the original post that's what I had before
     
  18. Strahan

    Benefactor

    Try dumping the path in a debug statement (the classType + "Weapons." + tier + "." + rarity part), check when weapons is null what the requested path was. Perhaps something went awry there.
     
  19. Thanks for the help everyone, issue solved