-1

I am using Bukkit API 1.8.3

I have this piece of code:

for(String vey : main.getConfig().getConfigurationSection("shopitems."+key+".enchantments").getKeys(false))
{
    Enchantment ench = Enchantment.getByName(vey);
    int level = main.getConfig().getInt("shopitems."+key+".enchantments."+vey);
    meta.addEnchant(ench, level, true);
}

This piece of code gives me a nullpointer pointing to the line that starts the for-loop iteration.

To try to fix this I have a Null checker:

if(main.getConfig().getConfigurationSection("shopitems."+key+".enchantments").getKeys(false)!=null)

After this null checker I then put the code above inside this if statement.

However I am getting a nullpointer on the line that is testing if that path is null

My question: Why is this not working and how can I fix it

NOTE: main.getConfig() which returns the FileConfiguration is not null I have tested an debugged this

JarFile
  • 318
  • 2
  • 8
  • 30
  • 2
    main could be null, main.getConfig() could be null, etc? – almightyGOSU Jul 30 '15 at 07:39
  • I edited my post, I have debugged that main.getConfig() is not null – JarFile Jul 30 '15 at 07:41
  • 1
    Have you tested what is returned by getConfigurationSection and getKeys? – JamesB Jul 30 '15 at 07:41
  • @JamesB I am testing if it is null – JarFile Jul 30 '15 at 07:43
  • @JarFile my point is that, the super long expression `main.getConfig().getConfigurationSection("shopitems."+key+".enchantments").getKeys(false)` can be null at different parts.. so `main.getConfig()` is not null, but what about `main.getConfig().getConfigurationSection("shopitems."+key+".enchantments")`? – almightyGOSU Jul 30 '15 at 07:43
  • My guess will be getConfigurationSection. – JamesB Jul 30 '15 at 07:43
  • @Gosu I see your point, but what I thought is that the getKeys() had to be there since you need to have that when iterating through that. That solved it – JarFile Jul 30 '15 at 07:46
  • duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Unihedron Jul 31 '15 at 09:57

1 Answers1

0
if(main.getConfig().getConfigurationSection("shopitems."+key+".enchantments").getKeys(false)!=null)

Is wrong, because when you try to getKeys(false) it returns null because getConfigurationSection() is already null. you can fix this by:

if(main.getConfig().getConfigurationSection("shopitems."+key+".enchantments") !=null);