spring-boot: NullPointerException when using Spring Data JPA native queries in Spring Boot 2.7.9 and 3.0.3

My build randomly stopped working today. After a couple hours of troubleshooting looking at some useless errors, my coworkers and I figured out that updating to 2.7.9 was what broke things. (we automatically update minor versions). This issue is very likely due to a sub-dependency issue, but I don’t have the time to figure out which one causes it.

Here’s the error (I’ve removed sensitive names)

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'basicSecurityConfiguration' defined in file [/app/build/classes/kotlin/main/com/focused/odin/BasicSecurityConfiguration.class]: Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'RedactedServiceName' defined in file [/app/build/classes/kotlin/main/com/focused/odin/service/RedactedServiceName.class]: Unsatisfied dependency expressed through constructor parameter 2; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redactedRepositoryName' defined in com.focused.odin.repository.unclassified.RedactedRepositoryName defined in @EnableJpaRepositories declared on JPAConfiguration: Invocation of init method failed; 
nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.focused.odin.repository.unclassified.RedactedRepositoryName.redactedRepsitoryFunction(long); 
Reason: Cannot invoke "String.contains(java.lang.CharSequence)" because "variable" is null; 
nested exception is java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "variable" is null
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.9.jar:2.7.9]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.9.jar:2.7.9]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.9.jar:2.7.9]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.9.jar:2.7.9]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.9.jar:2.7.9]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.9.jar:2.7.9]

there’s a bunch of caused by caused by caused by. But the last one is this:

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) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.DefaultQueryEnhancer.createCountQueryFor(DefaultQueryEnhancer.java:49) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.StringQuery.deriveCountQuery(StringQuery.java:119) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.<init>(AbstractStringBasedJpaQuery.java:72) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.NativeJpaQuery.<init>(NativeJpaQuery.java:53) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:51) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:169) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:253) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93) ~[spring-data-jpa-2.7.8.jar:2.7.8]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103) ~[spring-data-commons-2.7.8.jar:2.7.8]
        ... 71 common frames omitted

Then the function on the repository that’s causing the issue looks something like this:

  @Modifying
  @Transactional
  @Query(
    value = """
      UPDATE redacted
      SET
        value = true
      WHERE
        fk_value = :id
      AND
        string_value = 'Password Expires Soon'
      AND
        boolean_value is false""",
    nativeQuery = true
  )
  fun redactedRepositoryFunction(id: Long)

I’m confident that it’s these nativeQuery repository functions that are breaking because if I comment out this function a different nativeQuery in a different repository causes the same error to throw.

I’m happy to help track this down however I can. I’d be surprised if I’m the only one who has/will run into this.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 21
  • Comments: 29 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks all and apologies for the inconvenience. I’ll close this one in favor of the Spring Data JPA issue which has addressed the problem. We’ll upgrade to their next release in due course.

It happens to me as well when i upgraded to sb 3.0.3

Shifted from 2.7.8 to 2.7.9. The same error in code

interface ItemRepository : JpaRepository<Item, UUID>, JpaSpecificationExecutor<Item> {
    @Query(value = "select concat('BS_', nextval('item_code_seq'))", nativeQuery = true)
    fun getNextCode(): String
}

produces error:

Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.lang.String studio.buket.platform.persistence.repository.ItemRepository.getNextCode(); Reason: Cannot invoke "String.contains(java.lang.CharSequence)" because "variable" is null; nested exception is java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "variable" is null
	at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.7.8.jar:2.7.8]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:107) ~[spring-data-commons-2.7.8.jar:2.7.8]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:95) ~[spring-data-commons-2.7.8.jar:2.7.8]
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na]
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]

@RajiniRengaraj-idexcel https://github.com/spring-projects/spring-data-jpa/issues/2812 shows that it was fixed for Spring Data JPA 2.7.9, which is part of the Spring Data BOM 2021.2.9 release. Spring Boot 2.7.10 will come out later this week with Spring Data BOM 2021.2.9, but until then you can use Spring Boot 2.7.9 and temporarily override the spring-data-bom.version Maven property to 2021.2.9.

spring-boot 2.7.9 has same issue with spring-data-jpa:2.7.8, is there 2.7.10 planned ?

We updated to 3.0.3 from 3.0.2 and we also have the same nativeQuery issue.

I am also facing the same issue it’s not working for update as well as delete nativeQuery.

very helpful I just upgraded from 2.7.9 to 2.7.13 that’s it

Thanks for trying to help, @prudhvi46, but there should no longer be any need to work around this problem by changing queries. The bug has been fixed in Spring Data JPA and the latest versions of Spring Boot 2.7.x and 3.0.x use a version of Spring Data JPA that contains the fix.

Works for me with 2.7.10.