spring-bootspring-data-jpanullpointerexceptionjpa-2.1java-17

Need Help : Caused by: java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "variable" is null


I am trying to upgrade my SpringBoot Version from 2.1.1 to 2.7.x ( 2.7.5){Java Version - 17}, When I try to Run my application , I am getting the following error message

Caused by: java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "variable" is null at org.springframework.data.jpa.repository.query.QueryUtils.createCountQueryFor(QueryUtils.java:607) at org.springframework.data.jpa.repository.query.DefaultQueryEnhancer.createCountQueryFor(DefaultQueryEnhancer.java:49) at org.springframework.data.jpa.repository.query.StringQuery.deriveCountQuery(StringQuery.java:119) at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.(AbstractStringBasedJpaQuery.java:72) at org.springframework.data.jpa.repository.query.NativeJpaQuery.(NativeJpaQuery.java:53) at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:51) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:169) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:253) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103) ... 60 common frames omitted

Here are my dependencies:


implementation 'org.springframework.boot:spring-boot:2.7.5'

    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-actuator')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-parent:2.7.5')

    implementation('org.springframework:spring-jdbc')
    implementation('org.springframework:spring-orm')
    implementation('org.springframework:spring-core')
    implementation('org.springframework:spring-beans')
    implementation('org.springframework:spring-webmvc')
    implementation('org.springframework:spring-web')
    implementation('org.springframework:spring-context')
    testImplementation 'org.springframework:spring-test:5.3.25'
    compileOnly group: 'javax.servlet', name: 'servlet-api', version: '2.5'
    runtime("org.springframework.boot:spring-boot-properties-migrator")
    compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.56'
    implementation group: 'org.hibernate', name: 'hibernate-core', version: '5.6.5.Final'
    implementation 'org.springframework.cloud:spring-cloud-context:3.1.0'
    implementation group: 'org.springframework.security', name: 'spring-security-core', version: '5.6.9'
    implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
    implementation group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.4.0.Final'
    implementation group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'
    implementation("net.bytebuddy:byte-buddy:1.14.0")
    compileOnly 'org.projectlombok:lombok:1.18.26'
    annotationProcessor 'org.projectlombok:lombok:1.18.26'

Looking for a fix for this. This block is throwing an error:

@Modifying
    @Transactional
    @Query(value = "INSERT INTO W (CREATED_DATE, PRODUCT_CODE, TENANT_ID, UPDATED_DATE, UUID, STORE_ID) SELECT TO_TIMESTAMP(:createdDate, :format), :productCode, :tenantId, TO_TIMESTAMP(:updatedDate, :format), :uuid, :storeId FROM W WHERE ROWNUM <= 1 AND NOT EXISTS (select * from W w2 WHERE UUID = :uuid  AND PRODUCT_CODE =:productCode)" ,nativeQuery = true)
    int insertIntoW(@Param("format") String format, @Param("createdDate")String createdDate, @Param("productCode")String productCode, @Param("tenantId")String tenantId, @Param("updatedDate") String updatedDate, @Param("uuid")String uuid, @Param("storeId") String storeId);


Solution

  • UPDATE

    The new version 2.7.10 of spring boot has been released, which resolves the problem. Either use spring-boot-dependencies in the maven dependencyManagement tag or the spring boot parent with version 2.7.10


    Solution with Spring boot version 2.7.9

    There seems to be an issue in spring-data-jpa:2.7.8 (this version is used by spring-boot-starter-data-jpa:2.7.9) which results in an error when parsing native queries. The error seems to be fixed in version 2.7.10. A temporary fix, which worked for me, is to overwrite the version of spring-data-jpa in the maven dependency management section as follows:

    <dependencyManagement>
        <dependencies>
          ...
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>2.7.10</version>
            </dependency>
          ...
        <dependencies>
    <dependencieManagement>
    

    This topic is also discussed here: https://github.com/spring-projects/spring-boot/issues/34363 and here: https://github.com/spring-projects/spring-data-jpa/issues/2812

    Keep in mind that this is a temporary fix. As soon as a new version of spring-boot-stater-data-jpa uses a newer version of spring-data-jpa, you should remove the override and use spring's version management for consistency.