Eclipse findbugs找出的bug案例说明

本文详细介绍了Eclipse FindBugs工具在代码审查中发现的一些典型错误,包括无效的null检查、冗余的new操作、未使用的变量赋值、类名与超类名冲突、字符串比较错误等问题,并提供了相应的修复建议和代码优化方法。
摘要由CSDN通过智能技术生成
说明:1.Bug是findbug Eclipse插件原生的bug信息描述,Confidence 是fingbug团队认为该代码导致bug的可能性。
        2.以下都是我使用findbug在公司项目中找到的一些bug,这里做一些中文的简短说明(不是翻译)
        3.篇幅可能会有点长,阅读时,大家可以通过ctrl+f根据关键字查找自己相关的bug
BUG-0001
Bug
: Field only ever set to null: com.bettersoft.admin.BtCorpManager.ps 

All writes to this field are of the constant value null, and thus all reads of the field will return null. Check for errors, or remove it if it is useless.
Confidence
: Normal, Rank: Troubling (12)
Pattern
: UWF_NULL_FIELD 
Type: UwF, Category: CORRECTNESS (Correctness)
代码片段:

?
1
2
3
4
5
6
7
8
9
10
public class BtCorpManager {
     private BtCorp btcorp= null ;
     private Connection con = null ;
     private Statement st = null ;
     private PreparedStatement ps = null ;
     private ResultSet rs = null ;
     private void setConnection(String centerno) throws Exception{
         //con = DBManager.getConnection(centerno);
         con = DBManager.getConnection();
     }

解释说明:在BtCorpManager类里面定了一个私有的成员变量PreparedStatement ps,但是这个成员变量ps在实例范围内没有得到任何的初始化(采用默认的构造方法),始终为null,所以在实例范围内使用该成员变量时,如果不先对其进行初始化操作或者无意识的行为忘了初始化操作,那肯定是要报空指针异常,所以这无疑是一个bug  
推荐修改:   自己看着办


BUG-0002
Bug: Nullcheck of form at line 36 of value previously dereferenced in com.bettersoft.admin.CorpEditAction.execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)

A value is checked here to see whether it is null, but this value can't be null because it was previously dereferenced and if it were null a null pointer exception would have occurred at the earlier dereference. Essentially, this code and the previous dereference disagree as to whether this value is allowed to be null. Either the check is redundant or the previous dereference is erroneous.

Confidence: High, Rank: Scary (9)
Pattern: RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE 
Type: RCN, Category: CORRECTNESS (Correctness)
代码片段:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
         //throw new UnsupportedOperationException("Method is not implemented");
         ActionErrors errors = new ActionErrors();
         CreateCorpActionForm createCorp = new CreateCorpActionForm();
         createCorp = (CreateCorpActionForm)form;
         
         
         CreateCorpActionForm webcorp= new CreateCorpActionForm();
         BudgetWebcorpManager budgetWebcorpManager= new BudgetWebcorpManager();
         webcorp=budgetWebcorpManager.getCWebcorp(createCorp.getId());
         createCorp.setFbsaddapproveid(webcorp.getFbsaddapproveid());
         createCorp.setFbsinputapproveid(webcorp.getFbsinputapproveid());
         createCorp.setFbsprocessapproveid(webcorp.getFbsprocessapproveid());
 
         boolean b= false ;
         if (createCorp!= null ){
解释说明:注意到有个局部变量   CreateCorpActionForm createCorp;再看下它的初始化过程,先是通过new给它分配了内存空间,紧接着有让它引用了了另一个未知的变量,这里说未知是指这个新的引用可能为空,显然   createCorp有可能指向一个空的地址,所以在接下来的引用中极可能报空指针异常(在引用之前不进行判空操作的话)!   在接下来的代码,如下
?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值