千寻简笔记已开源,Gitee与GitHub搜索chihiro-notes
,包含笔记源文件.md
,以及PDF版本方便阅读,且是用了精美主题,阅读体验更佳,如果文章对你有帮助请帮我点一个Star
~
对象空指针是指一个指针变量指向了内存中的空地址,也就是没有指向任何有效对象的地址。在许多编程语言中,空指针通常用特殊的值(例如NULL、nil、None等)表示。
当你使用一个空指针来访问对象的成员或调用对象的方法时,会导致空指针异常(Null Pointer Exception)或类似的错误。这是因为空指针并没有指向有效的对象,无法执行相应的操作。
空指针错误通常是由以下几种情况引起的:
为了避免空指针错误,你可以在使用指针之前进行有效性检查,或者使用安全的编程技术,如空指针检查、异常处理等。
/**
* 根据id查询文章列表
*
* @param article 文章
* @return 文章
*/
@Override
public Article getArticleById(Long id)
{
Article article = baseMapper.selectArticleById(id);
AssertUtils.isNotEmpty(article,"抛出异常");
return article;
}
package com.torinosrc.common.utils.star;
import cn.hutool.core.util.ObjectUtil;
import com.torinosrc.common.exception.ServiceException;
import java.text.MessageFormat;
import java.util.Objects;
/**
* 校验工具类
*/
public class AssertUtil {
//如果不是true,则抛异常
public static void isTrue(boolean expression, String msg) {
if (!expression) {
throwException(msg);
}
}
public static void isTrue(boolean expression, ErrorEnum errorEnum, Object... args) {
if (!expression) {
throwException(errorEnum, args);
}
}
//如果是true,则抛异常
public static void isFalse(boolean expression, String msg) {
if (expression) {
throwException(msg);
}
}
//如果是true,则抛异常
public static void isFalse(boolean expression, ErrorEnum errorEnum, Object... args) {
if (expression) {
throwException(errorEnum, args);
}
}
//如果不是非空对象,则抛异常
public static void isNotEmpty(Object obj, String msg) {
if (isEmpty(obj)) {
throwException(msg);
}
}
//如果不是非空对象,则抛异常
public static void isNotEmpty(Object obj, ErrorEnum errorEnum, Object... args) {
if (isEmpty(obj)) {
throwException(errorEnum, args);
}
}
//如果不是非空对象,则抛异常
public static void isEmpty(Object obj, String msg) {
if (!isEmpty(obj)) {
throwException(msg);
}
}
public static void equal(Object o1, Object o2, String msg) {
if (!ObjectUtil.equal(o1, o2)) {
throwException(msg);
}
}
public static void notEqual(Object o1, Object o2, String msg) {
if (ObjectUtil.equal(o1, o2)) {
throwException(msg);
}
}
private static boolean isEmpty(Object obj) {
return ObjectUtil.isEmpty(obj);
}
private static void throwException(String msg) {
throwException(null, msg);
}
private static void throwException(ErrorEnum errorEnum, Object... arg) {
if (Objects.isNull(errorEnum)) {
errorEnum = BusinessErrorEnum.BUSINESS_ERROR;
}
throw new ServiceException(errorEnum.getErrorCode(), MessageFormat.format(errorEnum.getErrorMsg(), arg));
}
}
package com.torinosrc.common.utils.star;
/**
* Description: 业务校验异常码
*/
public enum BusinessErrorEnum implements ErrorEnum {
//==================================common==================================
BUSINESS_ERROR(1001, "{0}"),
//==================================chat==================================
SYSTEM_ERROR(1001, "系统出小差了,请稍后再试哦~~"),
;
private Integer code;
private String msg;
BusinessErrorEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public Integer getErrorCode() {
return code;
}
@Override
public String getErrorMsg() {
return msg;
}
public Integer getCode() {
return code;
}
public BusinessErrorEnum setCode(Integer code) {
this.code = code;
return this;
}
public String getMsg() {
return msg;
}
public BusinessErrorEnum setMsg(String msg) {
this.msg = msg;
return this;
}
}
package com.torinosrc.common.exception;
/**
* 业务异常
*
* @author ruoyi
*/
public final class ServiceException extends RuntimeException
{
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 错误明细,内部调试错误
*
* 和 {@link CommonResult#getDetailMessage()} 一致的设计
*/
private String detailMessage;
/**
* 空构造方法,避免反序列化问题
*/
public ServiceException()
{
}
public ServiceException(String message) {
this.message = message;
}
public ServiceException(String message, Integer code) {
this.message = message;
this.code = code;
}
// 增加方法1
public ServiceException(Integer errorCode, String errorMsg) {
super(errorMsg);
this.code = errorCode;
this.message = errorMsg;
}
// 增加方法2
public ServiceException(Integer errorCode, String errorMsg, Throwable cause) {
super(errorMsg, cause);
this.code = errorCode;
this.message = errorMsg;
}
public String getDetailMessage() {
return detailMessage;
}
public String getMessage() {
return message;
}
public Integer getCode()
{
return code;
}
public ServiceException setMessage(String message)
{
this.message = message;
return this;
}
public ServiceException setDetailMessage(String detailMessage)
{
this.detailMessage = detailMessage;
return this;
}
}
package com.torinosrc.common.utils.star;
public interface ErrorEnum {
Integer getErrorCode();
String getErrorMsg();
}
下面提供一个空指针校验工具类:
/**
* 根据id查询文章列表
*
* @param article 文章
* @return 文章
*/
@Override
public Article getArticleById(Long id)
{
Article article = baseMapper.selectArticleById(id);
AssertUtils.isNotEmpty(article,"抛出异常");
return article;
}
import cn.hutool.core.util.ObjectUtil;
import java.text.MessageFormat;
import java.util.Objects;
/**
* 校验工具类
*/
public class AssertUtil {
//如果不是true,则抛异常
public static void isTrue(boolean expression, String msg) {
if (!expression) {
throwException(msg);
}
}
public static void isTrue(boolean expression, ErrorEnum errorEnum, Object... args) {
if (!expression) {
throwException(errorEnum, args);
}
}
//如果是true,则抛异常
public static void isFalse(boolean expression, String msg) {
if (expression) {
throwException(msg);
}
}
//如果是true,则抛异常
public static void isFalse(boolean expression, ErrorEnum errorEnum, Object... args) {
if (expression) {
throwException(errorEnum, args);
}
}
//如果不是非空对象,则抛异常
public static void isNotEmpty(Object obj, String msg) {
if (isEmpty(obj)) {
throwException(msg);
}
}
//如果不是非空对象,则抛异常
public static void isNotEmpty(Object obj, ErrorEnum errorEnum, Object... args) {
if (isEmpty(obj)) {
throwException(errorEnum, args);
}
}
//如果不是非空对象,则抛异常
public static void isEmpty(Object obj, String msg) {
if (!isEmpty(obj)) {
throwException(msg);
}
}
public static void equal(Object o1, Object o2, String msg) {
if (!ObjectUtil.equal(o1, o2)) {
throwException(msg);
}
}
public static void notEqual(Object o1, Object o2, String msg) {
if (ObjectUtil.equal(o1, o2)) {
throwException(msg);
}
}
private static boolean isEmpty(Object obj) {
return ObjectUtil.isEmpty(obj);
}
private static void throwException(String msg) {
throwException(null, msg);
}
private static void throwException(ErrorEnum errorEnum, Object... arg) {
if (Objects.isNull(errorEnum)) {
errorEnum = BusinessErrorEnum.BUSINESS_ERROR;
}
throw new BusinessException(errorEnum.getErrorCode(), MessageFormat.format(errorEnum.getErrorMsg(), arg));
}
}
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* Description: 业务校验异常码
* Author: abin
* Date: 2023-03-26
*/
@AllArgsConstructor
@Getter
public enum BusinessErrorEnum implements ErrorEnum {
//==================================common==================================
BUSINESS_ERROR(1001, "{0}"),
//==================================user==================================
//==================================chat==================================
SYSTEM_ERROR(1001, "系统出小差了,请稍后再试哦~~"),
;
private Integer code;
private String msg;
@Override
public Integer getErrorCode() {
return code;
}
@Override
public String getErrorMsg() {
return msg;
}
}
import lombok.Data;
@Data
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
protected Integer errorCode;
/**
* 错误信息
*/
protected String errorMsg;
public BusinessException() {
super();
}
public BusinessException(String errorMsg) {
super(errorMsg);
this.errorMsg = errorMsg;
}
public BusinessException(Integer errorCode, String errorMsg) {
super(errorMsg);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BusinessException(Integer errorCode, String errorMsg, Throwable cause) {
super(errorMsg, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BusinessException(ErrorEnum error) {
super(error.getErrorMsg());
this.errorCode = error.getErrorCode();
this.errorMsg = error.getErrorMsg();
}
@Override
public String getMessage() {
return errorMsg;
}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
public interface ErrorEnum {
Integer getErrorCode();
String getErrorMsg();
}