Spring-Boot @Autowired Interface (Crudrepository) in My Service Class => Null Pointer Exception

spring-boot @autowired interface (CrudRepository) in my Service class => Null pointer exception

Suppose you have interface be like this:

public interface JobHistoryRepository extends CrudRepository<JobHistory, Long> {

List<JobHistory> findByJobGroup(String jobGroup);
JobHistory findById (Integer id);
List<JobHistory> findByStateAndJobGroup(String state, String jobGroup);
JobHistory findByJobNameAndJobGroupAndIdTrigger (String jobName, String jobGroup, String idTrigger);
}

Then create a service class like :

@Service
public class JobHistoryService {

private final JobHistoryRepository jobHistoryRepository;

public JobHistoryService (final JobHistoryRepository jobHistoryRepository)
{
this.jobHistoryRepository = jobHistoryRepository;
}

public void setReadyJob(String jobId, String tenant, String idTrigger) {
JobHistory jobHistory = new JobHistory();
jobHistory.setJobName(jobId);
jobHistory.setJobGroup(tenant);
jobHistory.setIdTrigger(idTrigger);
jobHistory.setIdUser("12345");
jobHistory.setState("Ready");
jobHistory.setDateCreation(new Date());

jobHistoryRepository.save(jobHistory);
}

Hope this helps.

Null Pointer Exception using Spring Boot service as a dependency in a batch job

The @Autowired annotation will not create your decryptor object, but will try to locate a Bean of that type and inject it in your DecryptionService. This means that you should not use a constructor to instantiate your DecryptionService. The DecryptionService should be defined as a @Service and injected in another component (@Component, @Controller etc.), from where you will invoke the specific methods for decrypting your data.

Why do I get NullPointerException with @Autowired Repository in Vaadin / SpringBoot application?

The issue in your code is that you are using bewohnerRepository in the constructor VaadinMainUI. In Spring Autowired fields are not available in constructors as field injection is done after constructor. So you need to change your code accordingly. You can e.g. have custom method where you do this and annotate it with @PostConstruct annotation, which makes Spring call it after field injection.

@PostConstruct
private void doGridSetup() {
Grid<Bewohner> grid = new Grid<Bewohner>(Bewohner.class);
Iterable<Bewohner> bewohnerList = bewohnerRepository.findAll();
grid.setItems((Collection<Bewohner>) bewohnerList);
add(grid);
}

Or, as annother alternative you can autowire bewohnerRepository as constructor parameter:

@Autowire
public VaadinMainUI(BewohnerRepository bewohnerRepository) {
Grid<Bewohner> grid = new Grid<Bewohner>(Bewohner.class);
Iterable<Bewohner> bewohnerList = bewohnerRepository.findAll();
grid.setItems((Collection<Bewohner>) bewohnerList);
add(grid);
}

Spring Data JPA repository throws null pointer

You currently instantiate HouseService by hand, not through Spring. In the houseService object that you created you have field HouseRepository houseRepository which is null, because you don't initialize it in any way. If you use Spring and its Autowired functionality, then you should get the instance of HouseService from Spring context. E.g. you can @Autowired it into your controller class.

NullPointerException on CrudRepository

In order to work properly with @Autowired you need to keep in mind that spring scans annotations to allow automatically classes load.

If you need to @Autowired some class, the class Test needs to have some annotation, that tells to Spring Boot to find it.

Your Test class need to have @Component, @Service, @Controller, @Repository, etc. Otherwise @Autowired will not work because Spring Boot not know your class and will not process it.

You can find some explanation here

Spring Data JPA Repository findAll() Null Pointer

The most likely cause is that speakerTopicsRepository is itself null, which is probably caused by forgetting to autowire it, e.g.

public class YourController {

@Autowired private SpeakerTopicsRepository speakerTopicsRepository;

@RequestMapping(value = "/lectures/{lectureId}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, SpeakerTopicLectures> getLecture(@PathVariable Long lectureId) {
// your method...
}

}

Spring Boot Autowiring Repository Always Null

The error is instantiating a PersonService manually in your controller, like thisPersonService ps = new PersonService().

For Spring to be able to autowire anything, you need to use the beans managed by it, so instead of creating a new PersonService on your controller, autowire it:

@Autowired
private PersonService personService;


Related Topics



Leave a reply



Submit