这里有两个空指针可能会发生的地方。一个是对象本身为空,一个是对象.get出来的字段为空。
@Override
public ExpertEntity getExpertById(Long id) {
ExpertEntity Expert = ExpertMapper.getObjectById(id);
if (Expert.getFilepath().length()>1) {
...
}
return Expert;
}
解决方案:StringUtils.isBlank();或者是StringUtils.isEmpty();都可以解决String的非空验证
@Override
public ExpertEntity getExpertById(Long id) {
ExpertEntity Expert = ExpertMapper.getObjectById(id);
if (StringUtils.isBlank(Expert.getFilepath())) {
...
}
return Expert;
}
看一下这个工具类的具体代码:
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}