本文介绍了在检查字段是否为null后抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Bukkit开发一个插件(),并在确认该插件不为空之后,它在第二行给了我NullPointerException

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;


推荐答案

您的代码!(说明.isEmpty())不会验证描述不为空。为此,您需要 description!= null

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

您的测试应为:

if (description != null && !description.isEmpty()) ...

这篇关于在检查字段是否为null后抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:52