在项目开发过程中,面对各种各样的对象,如果稍不注意,就会发生NULL空指针报错;是不是很烦恼,特别是对重要的参数判读;
经过总结,把各种类型的空判断进行了简单的封装,对新手还是很方便的;
package com.xt.shop.until;
import java.util.List;
/**
* <p>判断 对象 是否为空
* <p>返回值:为空 ? true : false
* <p>创建人:geYang
* <p>创建时间:2017.8.1
*/
public class IsNull {
/**
* 判断字符串是否为空
* */
public static boolean isNull(String str){
if(str!=null){
str = str.trim();
}
return str == null || str.isEmpty();
}
/**
* 判断List数组是否为空
* */
public static boolean isNull(List<?> list){
return list == null || list.isEmpty();
}
/**
* 判断Integer数组是否为空
* */
public static boolean isNull(Integer[] arr){
return arr==null || arr.length<1;
}
/**
* 判断整数是否为空(ID)
* */
public static boolean isNull(Integer num){
return num==null || num<1;
}
/**
* 判断Double是否为空(金额)
* */
public static boolean isNull(Double num){
return num==null || num<1;
}
/**
*<p>方法说明: TODO 测试测试
**/
public static void main(String[] args) {
String n = " ";
System.out.println(isNull(n));
}
}
就像这样,大家还可以根据自己的需要继续添加;