改findbogs碰到的两个问题,一个是关于IO流,一个是关于空指针检查异常。

1.NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE

前面代码略。。。

 File crFile = new File(crFilelocalPath);
if (crFile.exists()&& crFile.isDirectory() && crFile.listFiles() != null
&& crFile.listFiles().length > 0) { //略。。。 } 后面略。。。。

原因分析:

问题出在 crFile.listFiles() ,google到,说是第一个 crFile.listFiles()判断不为null,但是第二个crFile.listFiles()还是可能会为null。(原文:If the first call of listFiles() is != null, that doesn't mean the second call is also != null.)

google原文见:https://sourceforge.net/p/findbugs/bugs/1468/

所以改为

前面略。。

     File crFile = new File(crFilelocalPath);
if (crFile.exists() && crFile.isDirectory()) {
File[] files = crFile.listFiles();
if (files != null && files.length > 0) {
//略。。。
}
}
后面略。。。

findbugs就不报错了。。。

2.OBL_UNSATISFIED_OBLIGATION

是关于IO流的关闭

findbugs报错的代码

 public static boolean storeFile2LocalPath(String fileName, String localPath, File fileInput) {
boolean success = false;
FileInputStream inputStream = null;
OutputStream os = null;
try {
inputStream = new FileInputStream(fileInput);
//保存到临时文件
byte[] bs = new byte[1024];// 1K的数据缓冲
int len;// 读取到的数据长度
// 输出的文件流保存到本地文件
File tempFile = new File(localPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
String filePath = tempFile.getPath() + File.separator + fileName;
os = new FileOutputStream(filePath);
log.info("storeFile2LocalPath() and filePath = " + filePath);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完毕,关闭所有链接
try {
if(os != null) {
os.close();
os = null;
}
if(inputStream != null){
inputStream.close();
inputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return success;
}

报错如下:

This method may fail to clean up (close, dispose of) a stream, 
database object, or other resource requiring an explicit cleanup 
operation.

In general, if a method opens a stream or other resource, the method 
should use a try/finally block to ensure that the stream or resource 
is cleaned up before the method returns.

This bug pattern is essentially the same as the **OS_OPEN_STREAM and 
ODR_OPEN_DATABASE_RESOURCE** bug patterns, but is based on a different 
(and hopefully better) static analysis technique. We are interested is 
getting feedback about the usefulness of this bug pattern. To send 
feedback, either: •send email to findbugs@cs.umd.edu •file a bug 
report: http://findbugs.sourceforge.net/reportingBugs.html

In particular, the false-positive suppression heuristics for this bug 
pattern have not been extensively tuned, so reports about false 
positives are helpful to us.

See Weimer and Necula, Finding and Preventing Run-Time Error Handling 
Mistakes, for a description of the analysis technique.

原因分析:

连续关闭两个流,在同一个finally快里,若第一个流close失败,出现异常时,会导致第二个流没有关闭。

所以改为如下方式,findbugs不报错了。

//将后面的finally块改为: 再嵌套一个finally块

finally {
// 完毕,关闭所有链接
try {
if (os != null) {
os.close();
os = null;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

原!findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE 和 OBL_UNSATISFIED_OBLIGATION的更多相关文章

  1. FindBugs插件的使用手册

    安装FindBugs直接查找eclipse的商店,查找spot Bugs 插件,安装即可 完成安装之后重启eclipse,右击项目文件或目录,会发现多了Findbugs的菜单: 使用Findbugs ...

  2. findBugs英文代号的对照表

    findBugs错误英文翻译rule.findbugs.IMSE_DONT_CATCH_IMSE.name=不良实践 - 捕获可疑IllegalMonitorStateException rule.f ...

  3. 使用SpotBugs/FindBugs进行代码检查

    原po:https://blog.csdn.net/zhangb00/article/details/8407070 SpotBugs 介绍 SpotBugs是Findbugs的继任者(Findbug ...

  4. Findbugs异常总汇

    FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...

  5. findbugs规则

    FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...

  6. FindBugs报错

    FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...

  7. 详解FindBugs的各项检测器 .

    FindBugs是一个静态分析工具,在程序不需运行的情况下,分析class文件,将字节码与一组缺陷模式进行对比,试图寻找真正的缺陷或者潜在的性能问题.本文档主要详细说明FindBugs 2.0.3版本 ...

  8. [Done]FindBugs: boxing/unboxing to parse a primitive

    在开发过程中遇到了以下问题: FindBugs: boxing/unboxing to parse a primitive 查看代码(左边是老代码,右边是新的): 问题出在 自动装箱和拆箱的检查. 参 ...

  9. MyEclipse使用总结——将原有的MyEclipse中的项目转成maven项目[转]

    前面一篇文章中我们了解了 在myeclipse中新建Maven框架的web项目 那么如果我们原来有一些项目现在想转成maven项目应该怎么做呢 我收集到了三种思路: 一.新建一个maven项目,把原项 ...

随机推荐

  1. android studio - 导入工程报错[Plugin with id 'com.android.application' not found]

    出错现象: 大概意思是找不到:com.android.application 插件,以上现象对于初学者来说会经常碰到,下面分析下产生的原因. 原因分析 首先来看看导入后的工程结构: 对于此工程结构,是 ...

  2. CCNA2.0笔记_IPv6

    IPv6地址表示方法: 连续的零字段可表示为:: (每个地址只能用一次) 示例: 2031:0000:130F:0000:0000:09C0:876A:130B –可表示为2031:0:130f::9 ...

  3. sitemesh 学习之 meta 引入

    在上篇笔记学习了sitemesh的基本用法,这里还有另一种用法 在sitemesh.jar有一个默认的sitemesh-default文件 ,这个文件是可以指定的 可以指定的文件名的sitemesh. ...

  4. phoneGap 3.5 eclipise 模拟器调试

    最近想搞phoneGap开发,可是一看 http://www.phonegapcn.com/ phoneGap中文网 FUCK .phoneGap 还在1.0.0 里混呢.现在phoneGap 3.5 ...

  5. java.lang.NoSuchMethodError: org.apache.spark.util.ThreadUtils$.newDae

    -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1. ...

  6. IntelliJ IDEA 注册码-使用帮助

    拷贝自http://idea.lanyus.com,但是内容有些老旧,有空了我更新一下. 激活 激活码激活 授权服务器激活 破解补丁激活 修改试用时间 激活码 激活码目前为博主分享的自用激活码,到期前 ...

  7. Jmeter实现对字符串加密

    最近测试移动端接口,但是请求内容是用MD5加密的,所以要先对请求内容进行加密,Jmeter内置的没有MD5加密方法,所以自己从网上copy了一份,实现了加密功能,以下是具体操作: 1.从网上copy了 ...

  8. 描述J2EE框架的多层结构,并简要说明各层的作用。

    描述J2EE框架的多层结构,并简要说明各层的作用. 解答: 1) Presentation layer(表示层) a. 表示逻辑(生成界面代码) b. 接收请求 c. 处理业务层抛出的异常 d. 负责 ...

  9. bootstrap基础学习九篇

    现在学学bootstrap响应式实用工具 Bootstrap 提供了一些辅助类,以便更快地实现对移动设备友好的开发.这些可以通过媒体查询结合大型.小型和中型设备,实现内容对设备的显示和隐藏. 需要谨慎 ...

  10. Android基站定位

    Android基站定位   一.通过手机信号获取基站信息 通过TelephonyManager 获取lac:mcc:mnc:cell-id(基站信息)的解释: MCC.Mobile Country C ...