[英]java.lang.NullPointerException: null when using Spring Boot
[英]java.lang.NullPointerException: null when I call a repository
下午好,我在這里嘗試做的是通過 CUSPP 驗證是否有一個 object 將成為我的 ID,但是這個驗證是在我的 @Controller 之外完成的,因此我想在我的 AffiliateActiveV 中進行,然后通過調用它AfiliadoActivoV obj = new AfiliadoActivoV()
然后用數據AfiliadoActivoV(cuspp)
填充它。
我需要在我的@Controller class 之外進行驗證,因為 SonarLint 告訴我 class 太大了。
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 甚至沒有嘗試在這里找到要注入的 bean。 Spring 會告訴您它是否嘗試注入 @Autowired 但找不到匹配的 bean。
這意味着AfiliadoActivoV
不會被 Springs Component-Scanning 拾取,它收集了所有 CDI-able 類。
使用@Component
或@Service
注釋您的 class
@Component public class AfiliadoActivoV {
確保在組件掃描期間可以找到 class。 如果您依賴基於注釋的配置,則您有一個 class ,它使用@SpringBootApplication
注釋。 默認情況下,SpringBoot 只會掃描這個目錄和所有子目錄的 Bean 類。
我想您的@SpringBootApplication
-class 位於AfiliadoActivoV
的子目錄或兄弟目錄中。
在這種情況下,您可以修改類結構或使用@ComponentScan("...")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.