I'm trying to save the parmaters from my json, but when i iterate through the json array. it always gives me an exception : "Cannot invoke "java.util.Iterator.hasNext()" because "" is null". It seems that my array is empty !!. I'm not sure what I'm doing wrong but I'm pretty sure I'm doing something wrong iterating through this array.
`
{
"personId" : "1087280200",
"invoiceDate" : "2020-10-12",
"invoices":[
{
"invoiceAmount" :"300",
"invoiceNumber" :"ex12222"
},
{
"invoiceAmount" :"100",
"invoiceNumber" :"ex12333"
}
],
"msdin" : "xxxxxxxxxx",
}
`
`
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class InvoiceDTO {
@NotNull(message = "PersonId can't be null")
@PersonId
private String personId;
@NotNull(message = "invoiceDate can't be null")
// @DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd"))
@PastOrPresent
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate invoiceDate;
private InvoicesDTO invoices;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class InvoicesDTO implements List<InvoicesDTO> {
@NotNull(message = "invoice Number can't be null")
private String invoiceNumber;
@NotNull(message = "invoiceAmount can't be null")
private Double invoiceAmount;
try {
//Add the new amount of the invoice to an existing debts
Optional<Debts> debts = debtsRepository.findDebtsByPersonIdAndOrganization_id(invoiceDTO.getPersonId(),organization.get().getId());
Optional<Madeen> madeenOptional = madeenRepository.findByPersonId(invoiceDTO.getPersonId());
List<InvoicesDTO> invoicesDTO = invoiceDTO.getInvoices();
for (InvoicesDTO invoices : invoicesDTO) { //The error is here
Debts newDebt = new Debts(); //Only debts
newDebt.setPersonId(invoiceDTO.getPersonId());
newDebt.setCreatedDate(LocalDate.now());
newDebt.setUpdatedDate(invoiceDTO.getInvoiceDate());
newDebt.setDebtAmount(invoices.getInvoiceAmount());
newDebt.setInvoiceNumber(invoices.getInvoiceNumber());
newDebt.setOrganization(organization.get());
debtsRepository.save(newDebt);
}
`
Any thoughts on what I did wrong iterating through the invoices object?
I tried multiple approuches with iterating through the invoices object, like:
while (invoiceDTO.getInvoices().iterator().hasNext()) {
and
`
for (InvoicesDTO invoicesDTO: invoiceDTO.getInvoices()
`
But it's clear to me that my problem with how am i iterating through this array.