用户名重复时抛出null异常

用户名重复时抛出null异常

相关截图:

http://img1.sycdn.imooc.com//climg/6059b39e0917f64213740272.jpghttp://img1.sycdn.imooc.com//climg/6059b4940970144d13800470.jpg

问题描述:

controller中判断用户名密码为空这些信息都能正确返回。但是用户名重复时就会抛出null异常,异常截图如上;
这个null异常是怎么回事?

相关代码:

package com.he.mall.controller;

import com.he.mall.common.ApiRestResponse;
import com.he.mall.exception.HeMallException;
import com.he.mall.exception.HeMallExceptionEnum;
import com.he.mall.model.pojo.User;
import com.he.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* 用户控制器
*/
@Controller
public class UserController {
@Autowired
UserService userService;

@GetMapping("/test")
@ResponseBody
public User personalPage() {
return userService.getUser();
}
@PostMapping("/register")
@ResponseBody
public ApiRestResponse register(@RequestParam("userName") String userName, @RequestParam("password") String password) throws HeMallException {
//userName判断是否为空
if (StringUtils.isEmpty(userName)) {
return ApiRestResponse.error(HeMallExceptionEnum.NEED_USERNAME);
}
//password判断是否为空
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(HeMallExceptionEnum.NEED_PASSWORD);
} else if (password.length() < 8) {
//密码长度不能少于8位
return ApiRestResponse.error(HeMallExceptionEnum.PASSWORD_TOO_SHORT);
}
//保存至数据库
userService.register(userName, password);
return ApiRestResponse.success();
}

}

相关代码:

package com.he.mall.exception;

public class HeMallException extends Exception {
private final Integer status;
private final String msg;

public HeMallException(Integer status, String msg) {
this.status = status;
this.msg = msg;
}

public HeMallException(HeMallExceptionEnum exceptionEnum) {
this(exceptionEnum.getStatus(), exceptionEnum.getMsg());
}

public Integer getStatus() {
return status;
}

public String getMsg() {
return msg;
}

}

相关代码:

package com.he.mall.exception;

/**
* 异常枚举
*/
public enum HeMallExceptionEnum {
NEED_USERNAME(10001,"用户名不能为空"),
NEED_PASSWORD(10002,"密码不能为空"),
PASSWORD_TOO_SHORT(10003,"密码长度不能少于8位"),
NAME_EXISTED(10004,"用户已存在,注册失败"),
INSERT_FAILDED(10005,"系统错误,注册失败");

Integer status;//异常状态代码
String msg;//异常信息

HeMallExceptionEnum(Integer status, String msg) {
this.status = status;
this.msg = msg;
}

public Integer getStatus() {
return status;
}

public void setStatus(Integer status) {
this.status = status;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
}

相关代码:

​package com.he.mall.service.impl;

import com.he.mall.exception.HeMallException;
import com.he.mall.exception.HeMallExceptionEnum;
import com.he.mall.model.dao.UserMapper;
import com.he.mall.model.pojo.User;
import com.he.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* UserService实现类
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User getUser() {
return userMapper.selectByPrimaryKey(1);
}

@Override
public void register(String userName, String password) throws HeMallException {
//查询是否存在相同userName
User userResult = userMapper.selectByUserName(userName);
if (userResult != null) {
throw new HeMallException(HeMallExceptionEnum.NAME_EXISTED);
}
//保存至数据库
User user = new User();
user.setUsername(userName);
user.setPassword(password);
int count = userMapper.insertSelective(user);
//保存失败时处理
if (count == 0) {
throw new HeMallException(HeMallExceptionEnum.INSERT_FAILDED);
}

}
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,程序在输出异常堆栈信息的时候,会调用HeMallException类中getMessage()方法。由于同学HeMallException类中没有getMessage()方法,所以会输出null。

建议同学将HeMallException类中属性msg改为message,并重新生成get方法。

参考代码如下:

http://img1.sycdn.imooc.com//climg/6059bda80978f7c606220467.jpg

祝学习愉快~

  • 谁叫我这么坏 提问者 #1

    程序在输出异常信息的时候会自动调用getMessage()方法,对于自定义异常来说就是调用自定义的getMessage方法。所以自定义异常类中必须要有getMessage()方法,是这样的吗?

    2021-03-23 18:21:07
  • 同学你好,是的,你的理解是正确的。

    祝学习愉快~

    2021-03-23 18:30:44
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师