【异常处理】Mybatis报错 source is null for getProperty(null, “length“)

发现问题

复制代码
      <select id="listArticle" resultType="top.ambtwill.blog.dao.pojo.Article">
            select * from ms_article
            <where>
                1=1
                <if test="categoryId != null">
                    and category_id=#{categoryId}
                </if>
                <if test="tagId != null">
                    and id in (select article_id from ms_article_tag where tag_id = #{tagId})
                </if>
                <if test=" month.length>0  and year.length>0">
                    and year(FROM_UNIXTIME(create_date/1000)) = #{year}
                    and month(FROM_UNIXTIME(create_date/1000)) = #{month}
                </if>
            </where>
            order by weight,create_date desc;
        </select>

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'month.length>0 and year.length>0'. 
    Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "length")
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
	at com.sun.proxy.$Proxy91.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:121)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:85)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
	at com.sun.proxy.$Proxy93.listArticle(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at com.sun.proxy.$Proxy94.listArticle(Unknown Source)
	at top.ambtwill.blog.service.impl.ArticleServiceImpl.listArticle(ArticleServiceImpl.java:59)

解决问题

首先来看这个例子

复制代码
    @org.junit.Test
    public void tt(){
        String a= null;
        System.out.println(a.length());
    }

控制台报错:java.lang.NullPointerException

也就是说:为Null的字符串调用length()函数会报空指针异常

所以:我们要先判断字符串是否为空

复制代码
     Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "length")

getProperty(null, "length") 是反射,相当于

    String s=null;
    int ans = s.length();

所以就报错啦,只需要在动态SQL里面判断条件多一个判断非空即可

复制代码
     <if test="month != null and month.length>0 and year != null and year.length>0">
相关推荐
嵌入式大圣1 分钟前
Neko虚拟浏览器远程协作方案:Docker+内网穿透技术部署实践
运维·docker·容器
玩转4G物联网3 分钟前
零基础玩转物联网-串口转以太网模块如何快速实现与TCP服务器通信
服务器·网络·物联网·网络协议·tcp/ip·http·fs100p
派阿喵搞电子21 分钟前
Ubuntu下有关UDP网络通信的指令
linux·服务器·网络
一叶知秋哈23 分钟前
Java应用Flink CDC监听MySQL数据变动内容输出到控制台
java·mysql·flink
dmy30 分钟前
n8n内网快速部署
运维·人工智能·程序员
程序员JerrySUN38 分钟前
全面理解 Linux 内核性能问题:分类、实战与调优策略
java·linux·运维·服务器·单片机
Theodore_102243 分钟前
大数据(2) 大数据处理架构Hadoop
大数据·服务器·hadoop·分布式·ubuntu·架构
米粉03051 小时前
深入剖析Nginx:从入门到高并发架构实战
java·运维·nginx·架构
huangyuchi.1 小时前
【Linux】LInux下第一个程序:进度条
linux·运维·服务器·笔记·进度条·c/c++
蔡蓝1 小时前
设计模式-建造者模式
服务器·设计模式·建造者模式