Null pointer access: The variable al can only be null at this location问题的解决
2017-11-22 21:48
555 查看
错误信息:Null Pointer Exception(空指针异常)
错误剖析:我从后台数据库查询得到的ArrayList<Book> al = findBySql(sql,Book.class);,然后用它再添加book空对象。运行程序时报错,浏览器报错空指针异常,eclipse显示黄色警告信息,也就是标题内容,中文翻译一下就是:“空指针入口:这变量al为空时才会出现这种情况”。所以当我们从后台数据去搜寻结果集为空的情况下,就会出现以上那种问题!
问题分析:al.add()下出现警告信息Null pointer access: The variable al can only be null at this location;刚开始是并注意到这个换色预警信号,一直想办法去处理它,通过变量替换,始终解决不掉;其实我也想到要去给它先弄个值进去,这样就不为空了,但是还是不行,不符合我的程序,那样就会更加复杂了,后来new一个ArrayList<Book>(),这样我就有对象了,也就不存在为空了,Java有时候就是这些地方挺耐磨的!往往就是一步之遥的差距,如果你不真正深刻的明白其中的原理,那是会专牛角尖的!
我获得变量ArrayList<Book>
al=null,仅仅是后台数据库关于次信息为空,此时该变量仅仅在栈中,值为null,此时并没有指向对象中的任何对象,但是我却用它添加对象,这是不可能完成的。
解决方案:ArrayList<File> fList=null改为:ArrayList<Book> fList=new ArrayList<Book>();
总结: 注意看警告信息的文字,然后结合实际Java开发编程原理。原理很重要,不要依葫芦画瓢,知其然更有利于编程的工作!!!
错误剖析:我从后台数据库查询得到的ArrayList<Book> al = findBySql(sql,Book.class);,然后用它再添加book空对象。运行程序时报错,浏览器报错空指针异常,eclipse显示黄色警告信息,也就是标题内容,中文翻译一下就是:“空指针入口:这变量al为空时才会出现这种情况”。所以当我们从后台数据去搜寻结果集为空的情况下,就会出现以上那种问题!
问题分析:al.add()下出现警告信息Null pointer access: The variable al can only be null at this location;刚开始是并注意到这个换色预警信号,一直想办法去处理它,通过变量替换,始终解决不掉;其实我也想到要去给它先弄个值进去,这样就不为空了,但是还是不行,不符合我的程序,那样就会更加复杂了,后来new一个ArrayList<Book>(),这样我就有对象了,也就不存在为空了,Java有时候就是这些地方挺耐磨的!往往就是一步之遥的差距,如果你不真正深刻的明白其中的原理,那是会专牛角尖的!
我获得变量ArrayList<Book>
al=null,仅仅是后台数据库关于次信息为空,此时该变量仅仅在栈中,值为null,此时并没有指向对象中的任何对象,但是我却用它添加对象,这是不可能完成的。
解决方案:ArrayList<File> fList=null改为:ArrayList<Book> fList=new ArrayList<Book>();
总结: 注意看警告信息的文字,然后结合实际Java开发编程原理。原理很重要,不要依葫芦画瓢,知其然更有利于编程的工作!!!
相关文章推荐
- Null pointer access: The variable number can only be null at this location。 错误解决
- Null pointer access: The variable sList can only be null at this location
- Null pointer access: The variable sb can only be null at this location
- Null pointer access: The variable condition can only be null at this location。
- Warn:Null pointer access: The variable addressList can only be null at this location
- Null pointer access: The variable temp can only be null at this location
- Null pointer access: The variable marketcommends can only be null at this location
- Eclipse 代码检测报 Potential null pointer access: The variable XX may be null at this location
- Variable ‘time_zone’ can’t be set to the value of ‘NULL’解决方法
- mysql:Variable 'character_set_client' can't be set to the value of 'NULL'解决
- java.lang.NullPointerException at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)问题解决
- 服务器的日志一直报Packet for query is too large (7632997 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.的解决方法
- 如何解决 "This function or variable may be unsafe" 的问题
- MySQL 解决数据导出问题:Using a password on the command line interface can be insecure.
- the breakpoint will not currently be hit. No executale code is currently loaded at this location
- ORA-25152: TEMPFILE cannot be dropped at this time问题解决
- the logging tag can be at most 23 character—解决log的TAG参数不能太长
- [mysql] 批量导入提示: Variable ‘character_set_client’ can’t be set to the value of ‘NULL
- Git出现error: Your local changes to the following files would be overwritten by merge: ... Please, commit your changes or stash them before you can merge.的问题解决(Git代码冲突)
- 解决VS2013中“This function or variable may be unsafe”的问题