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
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()));
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.
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.
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.
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
Well, the two things that could be null are the ItemConfig and the weapons ConfigurationSection. Put some debugging in to determine which is null.
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?
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.