Java 使用接口时出现 NullReferenceException
Java NullReferenceException when using interface
我有一个 Spring REST API 我正在开发它由于某种原因不断返回 NullReferenceException。我不知道为什么。这是我的代码:
接口:
public interface InitiativesService {
InitiativeDto createInitiative(InitiativeDto initiativeDto);
}
实现它的class:
public class InitiativeServiceImpl implements InitiativesService {
private final InitiativeRepository initiativeRepository;
@Autowired
public InitiativeServiceImpl(InitiativeRepository initiativeRepository)
{
this.initiativeRepository = initiativeRepository;
}
@Override
public InitiativeDto createInitiative(InitiativeDto initiativeDto) {
initiativeRepository.Save(initiativeDto);
}
用于与 dB 通信的存储库 class:
@Repository
public interface InitiativeRepository extends JpaRepository<Initiative, Long> {}
最后是控制器:
public class InitiativesController {
private InitiativesService initiativesService;
public InitiativesController(InitiativesService initiativesService) {
this.initiativesService = initiativesService;
}
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<InitiativeDto> createInitiative(@Valid @RequestBody InitiativeDto initiative) {
log.info("Saving");
InitiativeDto createdInitiative = initiativesService.createInitiative(initiative);
return ResponseEntity.status(HttpStatus.CREATED).body(createdInitiative);
}
当我尝试 运行 并通过 Postman 进行测试时,它 returns 出现 NullPointerException。我调试它,我可以看到 initiativesService 为空。我很困惑,因为我有相同的创建用户的实现,它在没有这个问题的情况下工作正常。谁能发现我在这里遇到的任何问题?
在您的构造函数上添加 @Autowired
注释以自动装配您的 initiativeService
@Autowired
public InitiativesController(InitiativesService initiativesService) {
this.initiativesService = initiativesService;
}
我有一个 Spring REST API 我正在开发它由于某种原因不断返回 NullReferenceException。我不知道为什么。这是我的代码:
接口:
public interface InitiativesService {
InitiativeDto createInitiative(InitiativeDto initiativeDto);
}
实现它的class:
public class InitiativeServiceImpl implements InitiativesService {
private final InitiativeRepository initiativeRepository;
@Autowired
public InitiativeServiceImpl(InitiativeRepository initiativeRepository)
{
this.initiativeRepository = initiativeRepository;
}
@Override
public InitiativeDto createInitiative(InitiativeDto initiativeDto) {
initiativeRepository.Save(initiativeDto);
}
用于与 dB 通信的存储库 class:
@Repository
public interface InitiativeRepository extends JpaRepository<Initiative, Long> {}
最后是控制器:
public class InitiativesController {
private InitiativesService initiativesService;
public InitiativesController(InitiativesService initiativesService) {
this.initiativesService = initiativesService;
}
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<InitiativeDto> createInitiative(@Valid @RequestBody InitiativeDto initiative) {
log.info("Saving");
InitiativeDto createdInitiative = initiativesService.createInitiative(initiative);
return ResponseEntity.status(HttpStatus.CREATED).body(createdInitiative);
}
当我尝试 运行 并通过 Postman 进行测试时,它 returns 出现 NullPointerException。我调试它,我可以看到 initiativesService 为空。我很困惑,因为我有相同的创建用户的实现,它在没有这个问题的情况下工作正常。谁能发现我在这里遇到的任何问题?
在您的构造函数上添加 @Autowired
注释以自动装配您的 initiativeService
@Autowired
public InitiativesController(InitiativesService initiativesService) {
this.initiativesService = initiativesService;
}