I am using Spring boot with PostgreSql. I created entities,controller,DTO and DAO package. I find NullPointerException at ConvertToEntity() method.
Client.class
@Entity
public class Client {
@Id
@GeneratedValue
private Long id_client;
@Column(unique=true)
private Long num_client;
@Pattern(regexp="^[A-Za-z ]+$")
@Size(max = 60)
private String nom_client;
@ManyToMany
@JoinTable(
name="CLIENT_PACK",
joinColumns=@JoinColumn(name="CLIENT_ID", referencedColumnName="id_client"),
inverseJoinColumns=@JoinColumn(name="PACK_ID", referencedColumnName="id_pack"))
private List<Pack> packs;
Pack.class
@Id
@GeneratedValue
private Long id_pack;
@NotNull
@Pattern(regexp="^[A-Za-z ]+$")
@Size(max = 60)
private String nom_pack;
@Size(max = 2000)
private String description;
@Min(0)
@Max(99)
private int nbre_max_service;
@Min(0)
@Max(9999)
private int kilometrage_max;
@ManyToMany(mappedBy="packs")
private List<Client>clients;
ClientDTO.class
public class ClientDTO {
private Long id_client;
private Long num_client;
private String nom_client;
private List<PackDTO> packs;
}
PackDTO.class
public class PackDTO {
private Long id_pack;
private String nom_pack;
private String description;
private int nbre_max_service;
private int kilometrage_max;}
ClientServiceImpl
@Transactional
@Service
public class ClientServiceImpl implements ClientService {
@Autowired
private PackService packService;
@Autowired
private ClientRepository clientRepository;
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
private ModelMapper modelMapper = new ModelMapper();
@Override
public Client convertToEntity(ClientDTO clientDTO) {
//Client client = modelMapper.map(clientDTO, Client.class);
Client client =new Client(clientDTO.getNum_client(),clientDTO.getNom_client());
List<Pack> l =new ArrayList<Pack>();
for (PackDTO r: clientDTO.getPacks())
{
Pack rdto=packService.convertToEntity(r);
l.add(rdto);
}
client.setPacks(l);
return client;
}
@Override
public ClientDTO saveClient(Client c) {
clientRepository.save(c);
return convertToDTO(c);
}
}
CLientController.class
@RestController
@RequestMapping(value = "/clients")
public class ClientController {
@Autowired
private ClientService clientService;
@RequestMapping(value="/add",method = RequestMethod.POST)
public ClientDTO addClient(@RequestBody ClientDTO ud) {
Client u= clientService.convertToEntity(ud);
return clientService.saveClient(u);
}
}
When I enter this URL : http://localhost:8070/clients/add
It show this error :
java.lang.NullPointerException: null
at com.gaConnecte.assistAuto.service.impl.ClientServiceImpl.convertToEntity...
any idea