Intellij Idea:合同条款'null - >失败'被违反了

时间:2015-03-12 21:58:39

标签: java intellij-idea

我在Intellij IDEA 14中收到此警告,我不清楚它是什么意思。

public class SubArray {

private final int[] array;
private final int left;
private final int right;
private final int sum;

public SubArray(int[] array, int left, int right) {
    this.array = array;
    this.left = left;
    this.right = right;
    int s = 0;
    for (int i = left; i <= right; i++) {
        s += array[i];
    }
    sum = s;
}

public int[] getArray() {
    return array;
}

public int getLeft() {
    return left;
}

public int getRight() {
    return right;
}

public int getSum() {
    return sum;
}

}

public static SubArray returnMax(SubArray ... subArrays) {
    if (subArrays == null || subArrays.length == 0) {
        throw new RuntimeException("No sub arrays provided");
    }
    SubArray max = subArrays[0];
    for (int i = 1; i < subArrays.length; i++) {
        if (subArrays[i].getSum() > max.getSum()) {
            max = subArrays[i];
        }
    }
    return max;
}

return max;发出警告。 我检查subArrays是null还是空。我认为这应该足够了。

2 个答案:

答案 0 :(得分:1)

它只是一个错误(https://youtrack.jetbrains.com/issue/IDEA-136079),将在14.1版中修复。对不起。您可以使用EAP版本(https://confluence.jetbrains.com/display/IDEADEV/IDEA+14.1+EAP)或通过警告上的Alt + Enter暂时禁用此检查。

答案 1 :(得分:0)

可以使用null元素设置数组,并且只检查subArrays是否为null。您还应该检查subArrays[0]subArrays[i]是否正确。