[英]Why is my string doesn't accept null?
I need the String receive the null value if it is not found in mapper.getChave is what is returned.
如果在mapper中找不到字符串,我需要String 接收null值 。getChave是返回的值。 What I do?
我所做的? If I only get
nullPointerException
如果我只得到
nullPointerException
for(String chave : linha.keySet()) {
//Processa chave
String novaChave = mapper.getChave(chave.trim());
if (!(novaChave == null || novaChave.isEmpty())) {
//Processa valor
String novoValor = linha.getString(chave);
temp.append(novaChave, novoValor);
}
else {
extras.append(chave, linha.getString(chave));
}
}
Log 日志记录
java.lang.NullPointerException
at oknok.validacao.readers.PlanilhaReader.processaAtributosPlanilha(PlanilhaReader.java:237)
Line 237 is 237行是
String novaChave = mapper.getChave(chave.trim());
**UPDATE : The first time the loop runs, i have a Nullpointer and
chave
contains a value **更新 :循环第一次运行时,我有一个Nullpointer,并且
chave
包含一个值
System.out.println(chave.isEmpty() + "\t" + chave + "\t" + chave.trim());
Output 输出量
false Veículo Veículo
You need to add null check for mapper
as well as chave
. 您需要为
mapper
和chave
添加null检查。
if (mapper!= null && chave != null && !"".equals(chave) {
// do something
}
mapper.getChave(chave.trim())
^ ^ possible places for NPE.
chave
或mapper
值很可能为null,并且您分别在它们上调用trim()
和.getChave()
导致nullpointer
You need to check whether chave
is null before trimming it or doing anything else (I'm assuming that mapper
is pre-initialised and not null, but you should check that too) 您需要先检查
chave
是否为null, 然后再进行修剪或执行其他任何操作(我假设mapper
是预先初始化的,而不是 null,但您也应该进行检查)
eg 例如
if (chave != null && !"".equals(chave.trim()) {
// now do something
}
You may find it easier (more intuitive) to use something like Apache Commons StringUtils.isNotBlank(String)
. 您可能会发现更容易(更直观)使用类似Apache Commons
StringUtils.isNotBlank(String)
。 See here for the doc. 有关文档,请参见此处 。
linha.keySet()
有一个空字符串引用。
按照代码将空字符串更改为“”:您可以将“”更改为任意内容
String novaChave = mapper.getChave((chave == null ? "" : chave).trim());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.