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.

Comment From: eu-rlarsen

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

Comment From: nibsirahsieu

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

Comment From: knoobie

See https://github.com/spring-projects/spring-data-jpa/issues/2812 for more information.

Comment From: aldingithub

Same problem with me, when I upgraded to SB 3.0.3.

Comment From: eu-rlarsen

See spring-projects/spring-data-jpa#2812 for more information.

Yes adding a countQuery fixes the issue (just tested it) - BUT I don't like to add config that I don't need.

Comment From: wilkinsona

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.

Comment From: akbarkhan9044

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

Comment From: akbarkhan9044

Why this has been closed, it's still an issue

Comment From: eu-rlarsen

Why this has been closed, it's still an issue

I think because this NPE is not in Spring Boot, but in a library - have a look here: https://github.com/spring-projects/spring-data-jpa/issues/2812

Comment From: wilkinsona

That's correct, @eu-rlarsen. Thanks for helping out.

@akbarkhan9044 We use the issue's labels to try to indicate why an issue has been closed. In this case it's labeled with "for: external-project". If you hover over the label, you can see its description:

For an external project and not something we can fix

Comment From: mohammadhejazii

i have same problem by updating to spring 3.0.3 and solve it by adding countQuery to @Query annotation

Comment From: maxixcom

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]

Comment From: volverinejr

Consulte spring-projects/spring-data-jpa#2812 para obter mais informações.

Sim, adicionar um countQuerycorrige o problema (apenas testei) - MAS não gosto de adicionar configurações que não preciso.

Pra mim funcionou!

Comment From: shaklenahmad

i have same problem by updating to spring 3.0.3 and solve it by adding countQuery to @Query annotation

hi abrar .. can u help me how can i fix this issue as i am new to Springboot

my query looks like this...

@Modifying
    @Query(
            value = "update customerfile c  set c.registrationNumber=:regNum where c.id =:id",
            nativeQuery = true)

plz help how u fix this issue??

Comment From: tillkuhn

Workarounds are no longer necessary for Spring Boot 3.x, since 3.0.4 was just released and fixes the NPE.

Comment From: hendisantika

OIC. 3.0.4 was just released and fixes the NPE.

Comment From: pavelorehov

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

Comment From: hendisantika

How about to upgrade into 3.0.4 @pavelorehov ?

Comment From: wilkinsona

@pavelorehov Yes. Please see the milestone page for details.

Comment From: RajiniRengaraj-idexcel

Hi @wilkinsona, I Am also facing the same issue after upgrading to version 2.7.9, so please let me know which spring boot version the above-mentioned issue was fixed and released.

Comment From: wilkinsona

A version of Spring Data that contains the fix will be included in this week's Spring Boot 2.7.10 release.

Comment From: breun

@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.

Comment From: RajiniRengaraj-idexcel

Thanks, @breun and @wilkinsona for quick reply. let me check out and update here

Comment From: lpandzic

Works for me with 2.7.10.

Comment From: prudhvi46

In order to fix this issue , please write in below way

@Query(value = "**select * from student s**",nativeQuery = true)
List<Student> findAllStudentsByNativeQuery();

instead of

@Query(value = "**select * from student**",nativeQuery = true)
List<Student> findAllStudentsByNativeQuery();

Comment From: wilkinsona

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.

Comment From: RajiniRengaraj-idexcel

@RajiniRengaraj-idexcel spring-projects/spring-data-jpa#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.

It's working now after upgrading spring boot version from 2.7.9 to 2.7.10

Comment From: samatovM

worked from 3.0.3 to 3.0.4

Comment From: ShravanKV2310

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