很多小伙伴在写程序的时候对Warning不是特别看重,以为对程序没什么影响,但是花了几个小时debug之后发现错误竟然是出现在一个warning上的,顿时感觉很迷,来看下面的例子。

    /*** Generate a poem.* * @param input string from which to create the poem* @return poem (as described above)*/public String poem(String input) {String poem = null;String[] split = input.split("\\s|\\n");String[] addStrings = null;for(int i=0;i<split.length-1;i++) {//addStrings[i] = new String();addStrings[i] = getBridgeWord(split[i], split[i+1]);}for(int i=0;i<split.length;i++) {if(i!=split.length-1) {poem = poem + split[i] +  " " + addStrings[i];}else {poem = poem + split[i];}}return poem;}

我在程序中定义了一个addStrings的字符串数组并初始化为null(即便IDE提示的默认初始化也是null),在后续的调用过程中发现当使用这个本地变量的时候总是会爆出一条

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


大概意思,由于空指针的访问,本地的这个变量只能是空,也就是说,我们后续对这个变量的一切赋值操作都将不会实现,因为只能是空,这就导致我们程序调试过程中看到的种种不如意。

针对我的程序,有一个很好的解决办法就是直接给字符串数组实例化。

String[] addStrings = new String[split.length-1];

比如像这样,当然如果你的程序中并不知道我需要建立一个多大的数组或许你用到了容器类型的诸如Map,List,Set等等的变量,那你只需要将它实例化为一个空表即可

List<String> list = new ArrayList<>();

这样后续调用其的各种方法都能具体实现了,但因为容器类的对象都是可变数据类型(mutable),我们的程序中能少用则少用。

Warning:Null pointer access: The variable addStrings can only be null at this location相关推荐

  1. Null pointer access: The variable xxx can only be null at this location 解决方案

    错误信息:java.lang.NullPointerException 运行前就有错误警告:Null pointer access: The variable student can only be ...

  2. Null pointer access: The variable al can only be null at this location问题的解决

    错误信息:Null Pointer Exception(空指针异常) 错误剖析:我从后台数据库查询得到的ArrayList<Book> al = findBySql(sql,Book.cl ...

  3. 空指针异常Null pointer access: The variable roleList can only be null at this location

    1.错误信息 如上图第56行警告:Null pointer access: The variable roleList can only be null at this location 代码运行时报 ...

  4. 特例模式(Special Case Pattern)与空对象模式(Null Pointer Pattern)—— 返回特例对象而非 null

    返回 null 值,基本上是在给自己增加工作量,也是给调用者添乱.只有一处没有检查返回的是否为 null,程序就会抛 NullPointerException 异常. 如果你打算在方法中返回 null ...

  5. VCS仿真warning:Individual field access not available for field XX. Accessing complete register instead

    这个warning会在对reg field读写时出现,告诉你不支持直接对reg field读写,让你对它上一级reg进行读写,但经过测试实际上是可以实现对reg field读写的. 如果需要去掉这个w ...

  6. Android - Warning:Not annotated parameter overrides @NonNull parameter

    Warning:Not annotated parameter overrides @NonNull parameter 本文地址:http://blog.csdn.net/caroline_wend ...

  7. leetcode报错:member access within null pointer of type struct ListNode

    leetcode报错:member access within null pointer of type 'struct ListNode'

  8. :runtime error: member access within null pointer of type ‘struct ListNode‘报错

    该问题为刷力扣时,常见报错. 错误原因:通常是之前为struct ListNode分配了内存,但是其中指针未分配地址,导致系统认为其为野指针. 解决方案:如果为空,就令其指向NULL 如果不为空就加入 ...

  9. 【Leetcode记录】runtime error: member access within null pointer of type ‘ListNode‘ (solution.cpp) SUMMA

    环形链表快慢指针: runtime error: member access within null pointer of type 'ListNode' (solution.cpp) SUMMARY ...

最新文章

  1. 文件的记录c语言程序,计算机二级-C语言-程序填空题-190110记录-文件写入与文件读出显示...
  2. FaaS、PaaS和无服务器体系结构的优势
  3. PHP 7安装使用体验,升级PHP要谨慎
  4. 公共界面_小区公共区域广告收益究竟归谁?
  5. jsp servlet中的过滤器Filter配置总结(转)
  6. 干,认识Audio框架还因此发现一个雷
  7. 在EORow或者VORow中对数据进行重复性校验
  8. java 嵌套listview_ListView嵌套GridView使用详解
  9. python从html中提取文本_使用Python从HTML中提取可读文本?
  10. Audio bringup I2C调试(三十一)
  11. 坑爹的libxml2 for mingw 编译
  12. 获取电脑上连接的USB打印机
  13. 小钛掐指一算,今年的尖货市场不简单 | 活动预告
  14. Websphere远程代码执行-CVE-2015-7450
  15. mysql删除表的命令
  16. Redis的五种数据结构原理分析(中)
  17. Linux串口信息查询
  18. BIST(build_in selftest)介绍
  19. windows 10必装的五款神器
  20. 【免费素材】必备国内外常用blender材质模型下载网站

热门文章

  1. java之xml编程
  2. c++ 单引和双引的区别
  3. 使用PowerShell脚本部署定时器到MOSS2010(原创)
  4. Android系统在超级终端下必会的命令大全(二)
  5. Android ListView 下拉刷新 上拉更多[实例]
  6. Python中的公共操作(运算符,公共方法,容器类型转换)
  7. 大数据开发笔记(八):Spark综合笔记总结
  8. 爬虫运行成功但没数据_我整来了几台服务器,就是为了给你演示一下分布式爬虫的整个过程...
  9. L2-011 玩转二叉树(建树+BFS)
  10. Linux学习6之软件包管理--yum在线管理