Home > Back-end >  how to solve Internal Server Error in spring data
how to solve Internal Server Error in spring data

Time:11-18

Hello I want to update this entity, but I get this error enter image description here

Thanks for your help!

@PostMapping("updateabitre/{id}")
public Arbitre updateArbitre(@PathVariable Long id, Arbitre newArbitre){
    ServiceArbitre serviceArbitre = null;
    Arbitre arbitre = serviceArbitre.getArbitreById(id);
    arbitre.setNomArbitre(newArbitre.getNomArbitre());
    arbitre.setNationnalite(newArbitre.getNationnalite());

    serviceArbitre.updateArbitre(arbitre);
    return arbitre;
}

CodePudding user response:

Here, you are setting serviceArbitre to null before immediately invoking one of its functions, hence the NullPointerException.

ServiceArbitre serviceArbitre = null;
Arbitre arbitre = serviceArbitre.getArbitreById(id);

Also, you may need to annotate updated entity parameter (Arbitre newArbitre) with @RequestBody, that is:

public Arbitre updateArbitre(@PathVariable Long id, @RequestBody Arbitre newArbitre){

Otherwise Spring may not know to map the HTTP request body to the object.

  • Related