1

Error description:

There was an unexpected error (type=Internal Server Error, status=500).
Cannot invoke "com.example.produits.service.StabiliteService.saveStabilite(com.example.produits.entities.Stabilite)" because "this.stabiliteService" is null
java.lang.NullPointerException: Cannot invoke "com.example.produits.service.StabiliteService.saveStabilite(com.example.produits.entities.Stabilite)" because "this.stabiliteService" is null

controller class

@Controller
public class StabiliteController {

    @Autowired(required = false)
    private StabiliteService stabiliteService;

    @RequestMapping("/createStabilite")
    public String createStabilite(ModelMap modelMap) {
        modelMap.addAttribute("stabilite", new Stabilite());
        modelMap.addAttribute("mode", "new");
        return "formStabilite";
    }

    @RequestMapping("/saveStabilite")
    public String saveStabilite(@Valid Stabilite stabilite, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return "formStabilite";

        stabiliteService.saveStabilite(stabilite);
        return "formStabilite";
    }

service class:

@Service
public interface StabiliteService {

    Stabilite saveStabilite(Stabilite s);

    Stabilite updateStabilite(Stabilite s);

    void deleteStabilite(Stabilite s);

    void deleteStabiliteById(Long id);

    Stabilite getStabilite(Long id);

    List<Stabilite> getAllStabilites();

    List<Stabilite> findByLibelleStabilite(String libelle);

    Page<Stabilite> getAllStabillitesParPage(int page, int size);
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    For some reason, `stabiliteService` has not been wired, and is `null`. Note that since you explicitly said `required = false` you don't get an error for the wiring failure. But the flip-side is that if you are going to do that, your code should be designed to "work" in the case that `stabiliteService` is `null`. (Hmmm is that a typo? Do you maybe mean `stabilizeService`? What is a "stabilite"???) – Stephen C Dec 28 '21 at 04:52
  • Terrible title. Rewrite to summarize your specific technical issue. – Basil Bourque Dec 28 '21 at 07:11

1 Answers1

1

Is StabiliteService in correct package to read all components? Try with below annotation with @SpringBootApplication

@ComponentScan(basePackages={
        "com.your.package.name", 
        "com.your.another.package.name"
})
ParthPatel
  • 114
  • 2
  • 18