-2

I am developing a plugin for Bukkit (http://bukkit.org) and after verifying that it is not null, it gives me a NullPointerException at the 2nd line

    String description = getConfig().getString("core.commands."+cmd+".description");
    if (!(description.isEmpty()))
          getCommand(cmd).setDescription(description);
    else 
          getLogger().warning("NO description assigned to: " + cmd);
    description = null;
StormeHawke
  • 5,987
  • 5
  • 45
  • 73
stasiomod
  • 21
  • 3
  • 3
    Verifying that *what* isn't null? You're verifying that `description` isn't *empty* - but if it's a null reference, that `isEmpty` call will throw. Or maybe `getCommand` returns null. Spacing your code out would make it easier to see where the error is - *and* make it much easier to read your code in general. – Jon Skeet Sep 06 '13 at 19:49
  • I already tried if (description != null) – stasiomod Sep 06 '13 at 19:50
  • And have you tried formatting the code more sensibly so that you can check whether it's the `if` condition or the resulting statement which throws the exception? – Jon Skeet Sep 06 '13 at 19:54
  • 1
    Possible 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) – Ferrybig Jan 11 '16 at 15:09

3 Answers3

2

isEmpty() is different from null check. isEmpty() checks is String is empty String (which means "").

You need to do something like

if (description != null && !description.isEmpty())
kosa
  • 65,990
  • 13
  • 130
  • 167
2

Your code !(description.isEmpty()) doesn't verify that description is not null. For that you need description != null.

Your test should be:

if (description != null && !description.isEmpty()) ...
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
0

I suspect the problem isn't in the if condition - but in the statement that follows. I would start by reformatting the code like this:

String description = getConfig().getString("core.commands." + cmd + ".description");
if (!description.isEmpty())
{
    getCommand(cmd).setDescription(description);
}
else
{
    getLogger().warning("NO description assigned to: " + cmd);
}

(I've removed the description = null; statement as it's almost certainly unnecessary.)

With that change, you'll be able to tell more about what's throwing the exception. You can go further:

String description = getConfig().getString("core.commands." + cmd + ".description");
if (!description.isEmpty())
{
    Command command = getCommand(cmd); // Or whatever type it is
    command.setDescription(description);
}
else
{
    getLogger().warning("NO description assigned to: " + cmd);
}

Aside from anything else, when you now step through the code in a debugger (assuming that's even feasible) you'll be able to tell whether command is null (which I suspect it is). If you can't use a debugger, you could at least add logging.

(Others have already suggested checking whether description is null, but it sounds like that isn't the problem, which is why I've suggested that the problem could be within the body of the if statement.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ja... I realized that it is the problem of the getCommand().ANYTHING(); So now i get to play the waiting game until the devs of this API fix it... – stasiomod Sep 06 '13 at 20:05
  • @stasiomod: Well that just means that `getCommand()` returns `null` - so you should look at *why* it's returning `null`. Perhaps your `cmd` variable doesn't have a suitable value? – Jon Skeet Sep 06 '13 at 20:06