findbugs检测出的问题(二)

1 Possible null pointer dereference in method on exception path

		List<District> districts = null;
		try {
			districts = this.getDistricts(ReaderConst.DESC);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (start >= districts.size()) {	//districts 可能是null
			tableData.setTotalCount(0);
			return tableData;
		}

 2 内部类没有引用外部类的属性/方法的时候,应该作为静态内部类。

This class is an inner class, but does not use its embedded reference to the object which created it.  This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.  If possible, the class should be made static.

 

3 包装类的比较应该使用 eueqls,要比较值类型,需要强制类型转换后再使用。

This method compares two reference values using the == or != operator, where the correct way to compare instances of this type is generally with the equals() method. It is possible to create distinct instances that are equal but do not compare as == since they are different objects. Examples of classes which should generally not be compared by reference are java.lang.Integer, java.lang.Float, etc.

例 getTypeCodeID() 和getSpecCodeID() 方法均返回Integer

		if (configReaderInfo.getTypeCodeID() == realReaderInfo.getTypeCodeID()
				&& configReaderInfo.getSpecCodeID() == realReaderInfo
						.getSpecCodeID()) {
			return true;
		}

  

public class NumberTest {
	public static void main(String[] args) {
		Integer a = new Integer(1);
		Integer b = new Integer(1);

		System.out.println(a == b);  //结果是false
	}
}

 

4 Write to static field from instance method
  不要通过实例方法给静态变量赋值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值