-1

Is this the correct way to use and initialize the Optional variables .. I get an error "variable might not be initialized " on all these variables when I call calculateAnnualCalculationTotal ..I want to use these variables in the annualCalculationSummaryDto if the insurance.product isn't abc or def otherwise those three fields that am trying to set in the annualCalculationSummaryDto should remain null ..

                 Optional<CalculatedPaymentsDto> calculatedPayments  ;
        Optional<PaidPaymentsDTO> paidPayments  ;
        Optional<CreatePaymentResponseDto> payment ;

        if (!insurance.getProduct().equalsIgnoreCase("abc") && !insurance.getProduct().equalsIgnoreCase("def")) {

            int totalAnnualCalculationSum = calculateAnnualCalculationTotal(insurance, requestDto.getAnnualCalculationYear(), calculatedPayments, paidPayments);
            payment = Optional.ofNullable(createPayment(getCreatePaymentRequestDto(insurance, calculatedPayments.get(), totalAnnualCalculationSum, requestDto.getAnnualCalculationYear())));
        }

        AnnualCalculationSummaryDto annualCalculationSummaryDto = new AnnualCalculationSummaryDto();
        if(calculatedPayments.isPresent() && paidPayments.isPresent() && payment.isPresent()){
            annualCalculationSummaryDto.setCalculatedPaymentsDto(calculatedPayments.get());
            annualCalculationSummaryDto.setPaidPaymentsDTO(paidPayments.get());
            annualCalculationSummaryDto.setPayments(payment.get());
        }
     annualCalculationSummaryDto.setParam1(inr.getProduct());
     annualCalculationSummaryDto.setParam2(requestDto.getTotal());
     annualCalculationSummaryRepository.save(annualCalculationSummaryDto);

............. ............. ..............

private int calculateAnnualCalculationTotal (Insurance insurance , int year , Optional<CalculatedPaymentsDto> calculatedPayments , Optional<PaidPaymentsDTO> paidPayments) {

        calculatedPayments = Optional.ofNullable(paymentService.getCalculatedPayments(insurance.getId(), year));

        paidPayments = Optional.ofNullable(paymentService.getPaidPayments(insurance.getInsuranceYears(), year));
       totalSum = calculatedPayments.get().getParam1()+ paidPayments.get().getParam2();
.....
}

Or is there a better way to write this ..

Naman
  • 27,789
  • 26
  • 218
  • 353
Jenny
  • 77
  • 1
  • 6
  • 1
    *Or is there a better way to write this* - it is. don't use Optional for this – Eugene Mar 22 '19 at 13:03
  • 1
    Java has no pass-by-reference, so you can’t abuse the method’s parameters as return values. You are trying to pass uninitialized variables to the method, which does not work and within the method, you are assigning values to parameter variables, which has no effect to the caller. – Holger Mar 22 '19 at 14:11
  • @Eugene , Optional.empty() did the trick .. but you say that a better way is to not use Optional ? .. how do you propose I do that in my code ? .. Only if !insurance.getProduct().equalsIgnoreCase("abc") && !insurance.getProduct().equalsIgnoreCase("def")) I want to set those optional variable and use it in annualCalculationSummaryDto. – Jenny Mar 25 '19 at 16:34

1 Answers1

1

Due to the initialization of payment inside an if-clause, it's not guaranteed to be initialized.
To initialize an Optional as null use Optional<T> myOptional = Optional.empty();

A nice article about Optionals

gkhaos
  • 694
  • 9
  • 20