1

I have a Spring REST API I am developing that keeps returning a NullReferenceException for some reason. I can't figure out why. Here is my code:

The interface:

public interface InitiativesService {
    InitiativeDto createInitiative(InitiativeDto initiativeDto);    
}

The class that implements it:

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);
    }

The Repository class for communicating to the dB:

@Repository
public interface InitiativeRepository extends JpaRepository<Initiative, Long> {}

And lastly the controller:

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);
  }

When I try running this and testing via Postman, it returns a NullPointerException. I debug it and I can see the initiativesService is null. I am confused as I have the same implementation for creating Users which works fine without this issue. Can anyone spot any problems I have here?

1
  • Which is it? NullReferenceException or NullPointerException? Commented Mar 2, 2021 at 4:54

1 Answer 1

1

Add @Autowired annotation on your constructor to autowire your initiativeService

@Autowired
public InitiativesController(InitiativesService initiativesService) {
    this.initiativesService = initiativesService;
}
Sign up to request clarification or add additional context in comments.

5 Comments

This should not be needed since Spring 4.x . It automatically autowires the constructor.
oh shoot really? I never knew that before. all my life i have been autowiring my services through my constructor using the @Autowired annotation
@DonatoAmasa it works now, thank you. I can see its on the other controller but didn't realise it wasn't here. Suspected it would be something small like this.
@DonatoAmasa Yeah! Here's the Spring 4.3 blog post for this feature: spring.io/blog/2016/03/04/…
Sure thing @Yohan! Alright thanks @Benjamin M for the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.