Null pointer access: The variable sList can only be null at this location

来源:互联网 发布:大道寺知世和知世公主 编辑:程序博客网 时间:2024/04/29 20:15


错误描述:nullPointException

错误背景:我创建了一临时变量ArrayList<File> fList=null,然后用它添加File对象。运行程序时报错,仅仅是浏览器报错,eclipse不报错。

问题分析:dList.add()下出现警告信息Null pointer access: The variable sList can only be null at this location;刚开始是并没有注意他,后来发现问题就出在他身上。

我创建临时变量ArrayList<File> fList=null,仅仅是声明了一变量,此时该变量仅仅在栈中,值为null,此时并没有指向对中的任何对象,但是我却用它添加对象,这是不可能完成的。

解决方案:ArrayList<File> fList=null改为:ArrayList<File> fList=new ArrayList<File>();

总结: 不要忽视警告信息,有时警告也是错误。

0 0
原创粉丝点击