我写了一个函数:
private static LinearFunction[] aproxFunction(List<Point> list) {
try{
int amountOfClusters = getAmountOfClusters(list);
//System.out.println(amountOfClusters); for debug
LinearFunction[] linear = new LinearFunction[amountOfClusters];
int[][] clusters = new int[amountOfClusters][2]; // 2nd field 0 == r, 1 == g, 2 == b
clusters = getClusters(list, amountOfClusters);
for(int i = 0; i < amountOfClusters; i++) {
List<Point> pointsList = new ArrayList<>(getPointsInCluster(list, clusters[i][0], convertIdToString(clusters[i][1])));
int[][] points = new int[pointsList.size()][3];
for(int j = 0; j < points.length; j++) {
points[j][0] = pointsList.get(j).getX();
points[j][1] = pointsList.get(j).getY();
points[j][2] = pointsList.get(j).getValue();
}
pointsToFile(pointsList, clusters[i][0], convertIdToString(clusters[i][1]), "_points_in_cluster_");
int[][] array = new int[2][2];
array = aprox(removeDuplicates(points));
if((array[1][0] - array[0][0]) == 0) {
linear[i].a = 0;
linear[i].b = 0;
linear[i].flag = true;
linear[i].c = array[0][0];
} else {
linear[i].a = (array[1][1] - array[0][1]) / (array[1][0] - array[0][0]);
linear[i].b = array[1][1] - array[1][0] * linear[i].a;
}
linear[i].cluster = clusters[i][0];
linear[i].id = convertIdToString(clusters[i][1]);
}
return linear;
} catch (Exception e) {
System.out.println("Error - aproxFunction: " + e.getMessage());
}
LinearFunction[] error = new LinearFunction[1];
error[0].a = -1;
return error;
}
,我不断收到division by null
或null
的错误消息。我找不到原因。
此功能的唯一划分发生在这里:
linear[i].a = (array[1][1] - array[0][1]) / (array[1][0] - array[0][0]);
但是在此之上,您需要检查是否为空除法,因此如何仍能得到该错误消息。
至于错误消息null
,我只是不知道它是什么意思。我读到它可能是由对象为null引起的,但是如何找出在哪里?
答案 0 :(得分:0)
您正在初始化LinearFunction
的数组,但没有初始化其中的每个LinearFunction
对象。
您尝试过吗:
private static LinearFunction[] aproxFunction(List<Point> list) {
try{
int amountOfClusters = getAmountOfClusters(list);
//System.out.println(amountOfClusters); for debug
LinearFunction[] linear = new LinearFunction[amountOfClusters];
int[][] clusters = new int[amountOfClusters][2]; // 2nd field 0 == r, 1 == g, 2 == b
clusters = getClusters(list, amountOfClusters);
for(int i = 0; i < amountOfClusters; i++) {
linear[i] = new LinearFunction()
...
答案 1 :(得分:0)
我这样解决了NullPointerException:
for(int i = 0; i < amountOfClusters; i++) {
LinearFunction lf = new LinearFunction();
List<Point> pointsList = new ArrayList<>(getPointsInCluster(list, clusters[i][0], convertIdToString(clusters[i][1])));
int[][] points = new int[pointsList.size()][3];
for(int j = 0; j < points.length; j++) {
points[j][0] = pointsList.get(j).getX();
points[j][1] = pointsList.get(j).getY();
points[j][2] = pointsList.get(j).getValue();
}
pointsToFile(pointsList, clusters[i][0], convertIdToString(clusters[i][1]), "_points_in_cluster_");
int[][] array = new int[2][2];
array = aprox(removeDuplicates(points));
if((array[1][0] - array[0][0]) == 0) {
lf.a = 0;
lf.b = 0;
lf.flag = true;
lf.c = array[0][0];
} else {
lf.a = (array[1][1] - array[0][1]) / (array[1][0] - array[0][0]);
lf.b = array[1][1] - array[1][0] * linear[i].a;
}
lf.cluster = clusters[i][0];
lf.id = convertIdToString(clusters[i][1]);
linear[i] = lf;
}
因此,我创建了与数组相同类的对象,并且仅在循环结束时才将其指向数组的元素。