gpt4 book ai didi

java.lang.NullPointerException : null when I call a repository

转载 作者:行者123 更新时间:2023-12-01 22:38:44 25 4
gpt4 key购买 nike

下午好,我在这里尝试做的是验证是否有一个通过 CUSPP 的对象将成为我的 ID,但此验证是在我的 @Controller 外部完成的,因此我想在我的 AffiliateActiveV 中执行此操作,然后通过 AfiliadoActivoV obj = new AfiliadoActivoV() 调用它,然后用数据 AfiliadoActivoV(cuspp) 填充它。

我需要在 @Controller 类之外进行验证,因为 SonarLint 告诉我该类变得太大了。

AfiliadoActivoRepository

@Repository
public interface AfiliadoActivoRepository extends CrudRepository<AfiliadoActivoEntity, String> {
}

AfiliadoActivoController

@RestController
@CrossOrigin(origins = "*")
public class CargaArchivoController {
@GetMapping(path = "/leertxt")
public @ResponseBody String leerArchivo() {
AfiliadoActivoV obj = new AfiliadoActivoV();
return obj.validacionCampos(item.cuspp) //This value comes from a reading made to a csv file - I have validated with a System.out.Print and if it has data
}
}

AfiliadoActivoV

public class AfiliadoActivoV {

@Autowired
AfiliadoActivoRepository crudAfiliadoActivo2;

public String validacionCampos(String cuspp) {
String respuesta="";
if(crudAfiliadoActivo2.existsById(cuspp)==true) {
respuesta= respuesta + " Error: CUSPP Duplicado";
}
}}

我附上了STS控制台中出现的错误 enter image description here

最佳答案

事实上,它只是一个空指针,这告诉您 Spring 甚至没有尝试找到要注入(inject)的 bean。 Spring 会告诉您它是否尝试注入(inject) @Autowired 但找不到匹配的 bean。

这意味着 Springs 组件扫描不会拾取 AfiliadoActivoV,该扫描会收集所有支持 CDI 的类。

  1. 使用 @Component@Service 注释您的类

    @Component
    public class AfiliadoActivoV {
  2. 确保在组件扫描期间可以找到该类。如果您依赖基于注释的配置,您将拥有一个用 @SpringBootApplication 注释的类。默认情况下,SpringBoot 将仅扫描此目录和所有子目录中的 Bean 类。

我想您的@SpringBootApplication类位于AfiliadoActivoV的子目录或同级目录中。

在这种情况下,您可以修改类结构或使用@ComponentScan("...")

关于java.lang.NullPointerException : null when I call a repository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58510764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com