-1

I wrote a function:

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

and I constantly receive the error messages of either division by null or just null. I can't find the reason for that. The only division in this function happens here:

linear[i].a = (array[1][1] - array[0][1]) / (array[1][0] - array[0][0]);

but above that you have a check for null division, so how can I still get that error message.

As for the error message null I just don't know what it means. I read that it might be caused by an objects being null but how to find out which and where?

2 Answers2

0

You are initialising an array of LinearFunctions, but you are not initialising each LinearFunction object inside it.

Have you tried this:

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()
        ...
pavlos163
  • 2,730
  • 4
  • 38
  • 82
0

I solved this NullPointerException like this:

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

So I created an object of the same class as the array, and only at the end of the loop I point it to an element of the array.