findbugs检测出的问题(二)

1 Possible null pointer dereference in method on exception path

		List 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
  不要通过实例方法给静态变量赋值。

你可能感兴趣的:(findbugs检测出的问题(二))