0

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.

TechNewPy
  • 23
  • 4
  • in class `InvoiceDTO` `invoices` is defined as `InvoicseDTO` but in the 2nd snippet, `invoiceDTO.getInvoices()` returns list. so which is it? – Sharon Ben Asher Nov 20 '22 at 07:50
  • So, I have two DTO's , Invoice and invoices. Invoice has a general info about a customers invoice but invoices it's where I can include as many invoices which include [ invoiceAmount and invoiceNumber]. I hope this clears it. @SharonBenAsher – TechNewPy Nov 20 '22 at 07:58
  • no, it does not clarify at all, take a closer look at my comment and at your posted code. there is a conflict in the definition of `invoices` variable – Sharon Ben Asher Nov 20 '22 at 07:59
  • also, you need to post the code that deserializes the json into the DTO – Sharon Ben Asher Nov 20 '22 at 08:00

0 Answers0