I am working on a spring's stock management application. I have a function that create an invoice (facture), it create the invoice with the id of the customer (client), and in it I have an other function that create the invoice details. this is my entities:
the invoice entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Facture implements Serializable {
public Facture(float d, float e, Date date, boolean b, Set<DetailFacture> detailFactures2, Client c1) {
// TODO Auto-generated constructor stub
}
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name="idFacture")
private Long idFacture;
@Column(name="montantRemise")
private float montantRemise;
@Column(name="montantFacture")
private float montantFacture;
@Temporal(TemporalType.DATE)
@Column(name="dateFacture")
private Date dateFacture;
@Column(name="active")
private boolean active;
@OneToMany(mappedBy="facture")
@JsonIgnore
private Set<DetailFacture> detailFactures;
@ManyToOne
@JsonIgnore
private Client client;
}
the invoice details entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class DetailFacture implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name="idDetailFacture")
private Long idDetailFacture;
@Column(name="qte")
private float qte;
@Column(name="prixTotal")
private float prixTotal;
@Column(name="pourcentageRemise")
private int pourcentageRemise;
@Column(name="montantRemise")
private int montantRemise;
@ManyToOne
@JsonIgnore
private Produit produit;
@ManyToOne
@JsonIgnore
private Facture facture;
}
the customer entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Client implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name="idClient")
private Long idClient;
@Column(name="nom")
private String nom;
@Column(name="prenom")
private String prenom;
@Temporal(TemporalType.DATE)
@Column(name="dateNaissance")
private Date dateNaissance;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
@Column(name="categorieClient")
private CategorieClient categorieClient;
@Column(name="profession")
private Profession profession;
@OneToMany(mappedBy="client")
private List<Facture> factures;
}
this is my function in the invoice service that create an invoice with the customer id:
@Override
@Transactional
public Facture addFactureClient(Facture f, Long idClient) {
// TODO Auto-generated method stub
Client client = clientRepository.findById(idClient).orElse(null);
f.setClient(client);
f.setDateFacture(new Date());
f.setActive(true);
Set<DetailFacture> detailfactures = f.getDetailFactures();
Facture facture = addDetailFacture(f, detailfactures);
return factureRepository.save(facture);
}
and this is the function that create the invoice details:
private Facture addDetailFacture(Facture f, Set<DetailFacture> detailfactures){
float montantFacture = 0;
float montantRemise = 0;
System.out.println(detailfactures);
for (DetailFacture detail : detailfactures){
Produit produit = produitRepository.findById(detail.getProduit().getIdProduit()).orElse(null);
float prixTotalDetail = detail.getQte() * produit.getPrixUnitaire();
float montantRemiseDetail = (prixTotalDetail * detail.getPourcentageRemise())/100;
float prixTotalDetailRemise = prixTotalDetail - montantRemiseDetail;
detail.setMontantRemise((int)montantRemiseDetail);
detail.setPrixTotal(prixTotalDetailRemise);
montantFacture = montantFacture + prixTotalDetailRemise;
montantRemise = montantRemise + montantRemiseDetail;
detail.setProduit(produit);
detail.setFacture(f);
detailFactureRepository.save(detail);
}
f.setMontantFacture(montantFacture);
f.setMontantRemise(montantRemise);
return f;
}
this is the function in my controller:
@ApiOperation(value = "Ajouter facture avec client")
@PostMapping("/add-facture/{idClient}")
@ResponseBody
Facture addFacture(@RequestBody Facture f, @PathVariable("idClient") long idClient) {
return factureService.addFactureClient(f, idClient);
}
this is how I am testing with postman:
the problem is when I create a new invoice it returns a NullPointerException in this line :
Facture facture = addDetailFacture(f, detailfactures);
why detailfactures returns null?