0

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

Amir Choubani
  • 483
  • 2
  • 8
  • 19
  • It is not a duplicate question ! – Amir Choubani Jul 18 '17 at 10:54
  • Which line does the NullPointerException occur? Please provide the entire stack trace of the exception. – deHaar Jul 18 '17 at 11:12
  • java.lang.NullPointerException: null at com.gaConnecte.assistAuto.service.impl.ClientServiceImpl.convertToEntity(ClientServiceImpl.java:44) ~[classes/:na] at com.gaConnecte.assistAuto.controller.ClientController.addClient(ClientController.java:27) – Amir Choubani Jul 18 '17 at 11:13
  • Well… Which line is Nr. 44 in ClientServiceImpl? – deHaar Jul 18 '17 at 11:17
  • I mean line 44 in your IDE / Editor... I cannot see the line numbers in your posts, sorry. E.g. in "for (PackDTO r: clientDTO.getPacks())" clientDTO could theoretically be null. – deHaar Jul 18 '17 at 11:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149474/discussion-between-amir-choubani-and-dehaar). – Amir Choubani Jul 18 '17 at 11:26

0 Answers0