首页 > 解决方案 > 在 findbug 中不断收到可能的空指针取消引用?

问题描述

不明白为什么我在 findbug 中不断收到可能的空指针取消引用?我看到了一些如何修复它的示例,但我仍然收到错误消息。请给我一些提示。


static final ConcurrentHashMap<WFGaugeFactor,AtomicDouble> GAUGE_FACTORS = new ConcurrentHashMap<>();

public void handleGauge(String name, List<Tag> tags, double value) {
        String gaugeKey = gaugeKey(name, tags);

        if (GAUGE_CACHE != null && !GAUGE_CACHE.containsKey(gaugeKey)) {
            GAUGE_CACHE.put(gaugeKey, new AtomicDouble());
        }
        AtomicDouble gaugeValue =  getGaugeValue(gaugeKey) == null ? new AtomicDouble() : getGaugeValue(gaugeKey);
        System.out.println("Value:" + gaugeValue.doubleValue());
}

    @CheckForNull
    private AtomicDouble getGaugeValue(String key) {
        if (GAUGE_CACHE != null && GAUGE_CACHE.get(key) != null) {
            return GAUGE_CACHE.get(key);
        } else {
            return new AtomicDouble();
        }
    }

错误显示:

Code    Warning
NP  Possible null pointer dereference in handleGauge(String, List, double) due to return value of called method
Details
NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE: Possible null pointer dereference due to return value of called method
The return value from a method is dereferenced without a null check, and the return value of that method is one that should generally be checked for null. This may lead to a NullPointerException when the code is executed.```

标签: javapointersnullfindbugs

解决方案


推荐阅读