Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

lambda

java-8

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.

like image 724
ThisIsNoZaku Avatar asked Sep 08 '25 10:09

ThisIsNoZaku


1 Answers

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.

like image 141
ThisIsNoZaku Avatar answered Sep 10 '25 06:09

ThisIsNoZaku