javamybatisspring-mybatis

MyBatis return both emptyList() and null


Here is my myBatis request which should return a List:

@Mapper
public interface ClientAccessMapper {
    List<ClientAccess> findByClientAndPartnerWithAutoRenewal(@Param("clientId") Long clientId,
                                                             @Param("partner") String partner,
                                                             @Param("autoRenewal") Boolean autoRenewal);
}

<select id="findByClientAndPartnerWithAutoRenewal" resultMap="ClientAccessResult">
        select * from client_access
        where client_id = #{clientId}
            and partner = #{partner}
        <if test="autoRenewal != null">
            and auto_renewal = #{autoRenewal}
        </if>
        order by id
    </select>

Sometimes the request returns "null" instead of an empty List. After getting I have a checking block:

final List<ClientAccess> clientAccesses = clientAccessMapper.findByClientAndPartnerWithAutoRenewal(client, partner, true);
        if (clientAccesses.isEmpty()) {/**/}

and my clientAccesses.isEmpty() SOMETIMES produces NPE because of null instead of a List with size = 0. What can be a problem?


Solution

  • Possible null pointer. Recommended use:

    CollectionUtils.isEmpty(priceList)