Possible null pointer dereference in SONAR

这一生的挚爱 提交于 2019-12-13 06:45:52

问题


I have tried to point critical issues in Sonar with the following code:

if (candidate.isDirectory() && candidate.canRead() && 
          TEMPLATE.equalsIgnoreCase(candidate.getName())) {  
  List<String> fileContentList = Arrays.asList(candidate.list());

I have also done the change below, but it still didn't work

if(null != Arrays.asList(candidate.list())){
  List<String> fileContentList = Arrays.asList(candidate.list());}

Please help.


回答1:


The error occures because candidate can be null. See https://www.owasp.org/index.php/Null_Dereference for a description.

You could do sth. like this:

List<String> fileContentList = candidate != null ? Arrays.asList(candidate.list()) : new ArrayList<>();


来源:https://stackoverflow.com/questions/37295215/possible-null-pointer-dereference-in-sonar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!