dimanche 28 juillet 2019

Spring boot java.lang.NullPointerException: null

I am trying to build Spring boot crud application with hibernate and rest Api.However when I try to run the app everything is working fine but console is displaying the following error java.lang.NullPointerException: null at io.javabrains.EmployerController.getAllEmployers(EmployerController.java:20) ~[classes/:na]

I tried to change the value however it didnt work

EmployerService.java

package io.javabrains;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import io.javabrains.Entity.Employer;

@Service
public class EmployerService {

    private Repository repository;

    public List<Employer>getAllEmployers(){
        List<Employer>employers = new ArrayList<>();
        repository.findAll()
        .forEach(employers::add);
        return employers;

    }

    public void addEmployer(Employer employer) {
        repository.save(employer);
    }







}

EmployerController.java

package io.javabrains;

import java.util.List;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.javabrains.Entity.Employer;

@RestController
public class EmployerController {

    private EmployerService service;


     @RequestMapping("/employer") 
     public List<Employer>getAllEmployers()
     {
         return  service.getAllEmployers();
}



    /*
     * @RequestMapping("/employer/{id}") public Employer getEmployer(@PathVariable
     * int id) { return service.getEmployer(id); }
     */

    @RequestMapping(method=RequestMethod.POST,value="/employer/create")
    public void addEmployer(@RequestBody Employer employer) {
        service.addEmployer(employer);

    }


}

....




Aucun commentaire:

Enregistrer un commentaire