为什么这个函数 return 可能是 null?

Why can this function return a null possibly?

假设我有一个 guava 辅助函数

private Function<Config, Predicate<String>> makeFunctionToCompareTwoPathStrings() throws ConfigException {
    return new Function<Config, Predicate<String>>() {
        @Override
        public Predicate<String> apply(final Config config) {
            try {
                final Path input = computationThatMightThrowAnException();
                return new Predicate<String>() {
                    @Override
                    public boolean apply(String actual) {
                        return input.toAbsolutePath().toString().equalsIgnoreCase(actual);
                    }
                };
            } catch (ConfigException | URISyntaxException weDontReallyCareAboutItForThisTest) {
                log.error(weDontReallyCareAboutItForThisTest.getMessage());
            }
            // should always return a predicate if nothing returned yet, simply return an alwaysFalse predicate
            return Predicates.alwaysFalse();
        }
    };
}

当我使用这个时:

编辑:

Config config = Config.getInstance();
makeFunctionToCompareTwoPathStrings().apply(config).apply(config.getInput());

我收到警告

Dereferencing possible null pointer

在最外层的这一行中 apply - 谓词中的那个 - 我不明白。

编辑:

有点多余但有用 Predicates.or 显然让分析器更清楚了:) :

Predicates.or(makeFunctionToCompareTwoPathStrings().apply(config)).apply(config.getInput());

我认为您收到的警告并不是来自对代码实际作用的非常明智的分析。它可能只使用 Function.apply() 方法上的注释,声明为 @Nullable,这意味着 apply() 是 allowed 到 return 无效的。您的具体功能没有,但代码检查器不够聪明,无法知道。