SSM框架controller中遇到java.lang.NullPointerException

目的:由于搜索条件多个且不确定,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对象却不行。

转载于:https://juejin.im/post/5c7d20bef265da2d9e17750f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值