目的:由于搜索条件多个且不确定,mybatis用trim、if标签解决了多个参数且不固定时的sql语句查询,但是到了控制器处,方法参数特意改为了 required=false,参数由string和int型变量组成。
@RequestMapping(value = "/userManager",method = {RequestMethod.GET,RequestMethod.POST})
public String userManager(Model model,
@RequestParam(value = "userName",required = false) String userName,
@RequestParam(value = "nickName",required = false) String nickName,
@RequestParam(value = "gender",required = false) Integer gender,
@RequestParam(value = "eMail",required = false) String eMail,
@RequestParam(value = "createTime",required = false) Date createTime,
@RequestParam(value = "userCode",required = false) Integer userCode){
/*if (userName==null&nickName==null&gender==0&eMail==null&createTime==null&userCode==0){
List<User> userList = userService.selectAll();
model.addAttribute("userList",userList);
return "/backend/userManager";
}*/
List<User> userList = userService.selectDynamic(userName, nickName, gender, eMail, createTime, userCode);
model.addAttribute("userList",userList);
Iterator<User> iterator = userList.iterator();
if (iterator.hasNext()){
System.out.println(iterator.next());
}else {
System.out.println("userList为空");
}
return "/backend/userManager";
}
复制代码
报错行内容
List<User> userList = userService.selectDynamic(userName, nickName, gender, eMail, createTime, userCode);
复制代码
查阅JDK1.8的NullPointerException API
public class NullPointerException extends RuntimeException当应用程序尝试在需要对象的情况下使用null时抛出。 这些包括:
- 调用一个null对象的实例方法。
- 访问或修改null对象的字段。
- 取null的长度,好像是一个数组。
- 访问或修改的时隙null就好像它是一个数组。
- 投掷null好像是一个Throwable价值。
应用程序应该抛出此类的实例以指示null对象的其他非法使用。 NullPointerException对象可以由虚拟机构建,就像suppression were disabled and/or the stack trace was not writable一样 。
从以下版本开始: JDK1.0 另请参见: Serialized Form
IDEA提示信息
Unboxing of 'gender' may produce 'NullPointerException' less... (Ctrl+F1) Inspection info: This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations. Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report NullPointerException (NPE) errors that might be produced. More complex contracts can be defined using @Contract annotation, for example: @Contract(", null -> null") — method returns null if its second argument is null @Contract(", null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise @Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it The inspection can be configured to use custom @Nullable @NotNull annotations (by default the ones from annotations.jar will be used)
首先,拆包gender可能造成nullpointerexception。
找到错误原因:只有controller的参数时interger类型,在dao层和service层,接口的参数类型都是int,需要用integer对象。
null可以赋值给Integer对象,int对象却不行。