Skip to content
Advertisement

‘OptionalDouble.getAsDouble()’ without ‘isPresent()’ check

I’ve seen a bunch of solutions for this question but no matter what I try, IDEA still reports an error.

Consider the following block:

JavaScript

This reports a warning of 'OptionalDouble.getAsDouble()' without 'isPresent()' check.

If I try this then it doesn’t compile:

JavaScript

Neither does this:

JavaScript

Or this:

JavaScript

While i know these optional doubles will never be null, i’d like to resolve them so there are no warnings.

Can anyone point out where i am going wrong?

Advertisement

Answer

JavaScript

orElseThrow returns a double, not an OptionalDouble. There’s no need to call getAsDouble().

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement