Reputation: 4673
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.
Upvotes: 0
Views: 573
Reputation: 18931
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.
Upvotes: 1
Reputation: 5351
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.
Upvotes: 0