0

Could You tell me whether i can create multidimensional array which is created with my objects in java?

In other words:

I have got this class:

public class ClosestObject {
public Double distance;
public String classes;

public ClosestObject() {
        this.distance=new Double(0);
        this.classes=new String("");
        // TODO Auto-generated constructor stub
    }   
}

And I want to create multidimensional array in another class in that way:

    ClosestObject[][] closestobj=new ClosestObject[query.length][learning_set.length];

    for(int i=0; i<query.length;i++)
    {
        for(int j=0;j<learning_set.length;j++)
        {
            closestobj[i][j].distance=Math.abs(query[i]-learning_set[j][0]);
            closestobj[i][j].classes=classes[j][0];
        }
    }

    for(int i=0;i<closestobj.length;i++)
    {
        for(int j=0;j<closestobj[i].length;j++)
        {
            System.out.println(closestobj[i][j].distance + " " + closestobj[i][j].classes);
        }
    }

Unfortunatelly I get this error:

Exception in thread "main" java.lang.NullPointerException
at KNN.Find_closest_distance(KNN.java:60)
at KNN.main(KNN.java:37)

Could someone tell me where i made mistake. Thank You in advance :)

Dominik
  • 1,233
  • 2
  • 14
  • 29
  • 1
    By default new array of objects is filled with `null`s (and null doesn't have any `distance` filed which is why you are getting NPE). So before you call `closestobj[i][j].distance=...` you need to first put new instances in that cell with `closestobj[i][j] = new ClosestObject ()`. – Pshemo Feb 03 '16 at 21:06
  • Can you add Your comment as the answer because You are right and i want to give you "right answer" tag :) – Dominik Feb 03 '16 at 21:08
  • It is common problem and it is already explained in one of answers from duplicate question (link is above your question - you may refresh your page to see it) so feel free to up-vote answer which explains it. – Pshemo Feb 03 '16 at 21:09
  • Thank you for your edit, but to me, your link is a general discussion of the NullPointerException and I have a specific case. – Dominik Feb 03 '16 at 21:13
  • Yes, it is general discussion but it contains explanation and potential solution to your problem: http://stackoverflow.com/a/23852556/1393766. I may try to find better candidate for duplicate if you wish. – Pshemo Feb 03 '16 at 21:16
  • No, You are right they are simmilar :) I have got only one request. Could You tell me how can i sort that multidimensional array ? Because i have to sort it by "public Double distance" ? I will be very thankfull. – Dominik Feb 03 '16 at 21:20
  • OK, my previous comment (now deleted but pointing to http://stackoverflow.com/q/4907683/1393766) could be based on false assumption. Could you explain how do you want to sort your array exactly? – Pshemo Feb 03 '16 at 21:26
  • Yeah, no problem. I am making my own implementation of kNN machine learning algorithm and I want to sort closestobj array by Double distance which is placed in ClosestObject class. In that case I don't know how to transport that to sorting function. Please tell me whether it is clear to You? – Dominik Feb 03 '16 at 21:32
  • Not quite. Could you post link to simple example with of unsorted and sorted 2d array? Or lets say that we have 2d array like `[[8,5], [9,2]]`. Do you want to order elements in each row like `[[5,8], [2,9]]` or you want to sort all elements like `[[2,5], [8,9]]` (this will allow moving elements between rows). – Pshemo Feb 03 '16 at 21:37
  • Ok so let me show it on the example – Dominik Feb 03 '16 at 21:39
  • I have got multidimensional array which contains objects. It looks like something like that : [[1object, 2object,3object], [4object,5object, 6object]...] 1object.distance = 16.1; 1object.classes = rose ; 2object.distance =56.2; 2object.classes = another flower; ... I want to sort objects inside each row of my array by distance. Do you understand? :-) – Dominik Feb 03 '16 at 21:54
  • 1
    OK so you only want to sort rows (first case of my example: `[[5,8], [2,9]]`). Are you familiar with `Arrays.sort(row, Comparator)` method? – Pshemo Feb 03 '16 at 21:58
  • I have tried that: Arrays.sort(closestobj[0]); but i get error: Exception in thread "main" java.lang.ClassCastException: ClosestObject cannot be cast to java.lang.Comparable at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source) at java.util.ComparableTimSort.sort(Unknown Source) at java.util.ComparableTimSort.sort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at KNN.Find_closest_distance(KNN.java:72) at KNN.main(KNN.java:40) – Dominik Feb 04 '16 at 09:42
  • @Pshemo I solve sorting by that: `for(int k=0;k() { @Override public int compare(ClosestObject entry1, ClosestObject entry2) { ClosestObject time1 = entry1; // System.out.println(time1); ClosestObject time2 = entry2; // System.out.println(time2.distance); return time1.distance.compareTo(time2.distance); } }); }` thank You very much for Your help :) – Dominik Feb 04 '16 at 10:17

0 Answers0