ThisIsNoZaku
ThisIsNoZaku

Reputation: 2443

Java Lambda in Intellij: Expected not null but the lambda body is not value-compatible

Trying to filter a collection using a stream, and trying to pass the following lambda to filter() a Set, which gives the arcane error in the title:

unmatchedIncomingFields.stream().filter(s-> s.matches(fieldMatchPattern))

Meanwhile, creating a Predicate object works:

unmatchedIncomingFields.stream().filter(new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.matches(fieldMatchPattern);
    }
});

According to the JLS, a lambda body is "value-compatible" if every control path returns a value. matches() always gets called and always returns a boolean, so I don't understand what the problem is.

I've also tried all kinds of variations of the same lambda- with and without parentheses and argument types and using expression and block-with-return bodies.

Upvotes: 4

Views: 6528

Answers (1)

ThisIsNoZaku
ThisIsNoZaku

Reputation: 2443

The issue looks to be incorrect, or at least somewhat misleading, error highlighting within IntelliJ, confusing where the actual error was.

The filter occurs within another lambda for a map() operation which I had not specified a return for yet, and for some reason, IntelliJ highlights the inner lambda for the filter, making it look like it is the one with the error.

Upvotes: 9

Related Questions