2

I'm trying to add an entity Projet which has a list property of another entity Sprint. These are the two entities

Projet:

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Getter
@Setter
public class Projet {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String title;
    private String description;
    @ManyToMany(mappedBy = "projets")
    private List<User> users;
    @OneToMany(mappedBy = "projet")
    @JsonIgnore
    private List<Sprint> sprints;
}

Sprint:

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.Date;

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Sprint {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String description;
    @Temporal(TemporalType.DATE)
    private Date startDate;
    @ManyToOne
    @JsonIgnore
    private Projet projet;
}

I have a repository interface, ProjetRepository:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import tn.esprit.spring.entities.Projet;

@Repository
public interface ProjetRepository extends JpaRepository<Projet, Integer> {
}

A service interface, IProjetService:

import tn.esprit.spring.entities.Projet;

public interface IProjetService {
    public Projet addProject (Projet project);
}

And a service implementation class, ProjetService:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tn.esprit.spring.entities.Projet;
import tn.esprit.spring.entities.Sprint;
import tn.esprit.spring.repositories.ProjetRepository;

import java.util.List;

@Service
public class ProjetService implements IProjetService{
    @Autowired
    ProjetRepository projetRepository;
    @Override
    public Projet addProject(Projet projet) {
        List<Sprint> sprints = projet.getSprints();
        for ( Sprint sprint: sprints) {
            sprint.setProjet(projet);
        }
        return projetRepository.save(projet);
    }
}

This is the controller, ProjetController:

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tn.esprit.spring.entities.Projet;
import tn.esprit.spring.services.IProjetService;

@RequiredArgsConstructor
@RequestMapping("/projet")
@RestController
public class ProjetController {
    @Autowired
    IProjetService projetService;
    @PostMapping("/addProjet")
    public Projet addProject (@RequestBody Projet project){
        return projetService.addProject(project);
    }
}

This is the test application, SpringTestApplication:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication

@EntityScan(basePackages = {"tn.esprit.spring.entities"})
@ComponentScan(basePackages = {"tn.esprit.spring.controllers", "tn.esprit.spring.services"
        , "tn.esprit.spring.repositories"})

@EnableJpaRepositories(basePackages = {"tn.esprit.spring.repositories"})

public class SpringTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringTestApplication.class, args);
    }

}

When testing the addProjet method on Postman on this url using Post method http://localhost:8085/Exam/projet/addProjet using this request body:

{
    "title": "MAP",
    "description": "Gestion de Mandats, Assignations et Projets",
    "sprints": [
        {
            "description": "Sprint Projets",
            "startDate": "2023-05-27"
        }
    ]
}

In Postman response body:

{
    "timestamp": "2023-05-17T13:45:51.279+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "path": "/Exam/projet/addProjet"
}

In IDE console:

java.lang.NullPointerException: null
    at tn.esprit.spring.services.ProjetService.addProject(ProjetService.java:21) ~[classes/:na]
    at tn.esprit.spring.controllers.ProjetController.addProject(ProjetController.java:20) ~[classes/:na]

Which points to here:

return projetRepository.save(projet);

I really don't understand what the issue is, could anyone please help ?

EDIT: I also get this error in console: 2023-05-17 14:56:42.938 ERROR 8448 --- [nio-8085-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/Exam] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
Moe_blg
  • 71
  • 4

0 Answers0