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
){
|