简体   繁体   中英

Intellij Idea: Contract clause 'null -> fail' is violated

I get this warning in Intellij IDEA 14 and I don't quiet understand what it means.

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;
}

The row return max; issues the warning. I check if subArrays is either null or empty. I think this should be enough.

It's just a bug ( https://youtrack.jetbrains.com/issue/IDEA-136079 ), which will be fixed in version 14.1. Sorry for that. You can use the EAP version ( https://confluence.jetbrains.com/display/IDEADEV/IDEA+14.1+EAP ) or just temporarily disable this inspection via Alt+Enter on the warning.

The array can be set with null elements and you are only checking if subArrays is null . You should also check if subArrays[0] and subArrays[i] are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM