-1

I have a hashmap containing 6 elements as I have - E : [x1:0.4, x2: 0.5 , x3: 0.56, x4: 0.45] -

What I'm trying to do is to over each element, until the E is empty.

The problem here is that it retrieves the first min, and stops, yet the E hashmap still contains elements, what is it that I'm missing HashMap E = new HashMap<>(); // not that E is not empty and it is instantiated above

HashSet<Integer> dataPoints = new HashSet(); //list of points for each xmin

            HashMap<Integer, List<Integer>> clusters = new HashMap<>();

            ArrayList<Integer> listt = new ArrayList<>();

            List<Integer> l = new ArrayList<>(); //list of points for each xmin
            System.out.println("size one :"+dataPoints.size());

            while (!E.isEmpty()) {
                int xMin = getKey(E, MinX(E, dataPoints));
                dataPoints.add(xMin);
                System.out.println("Xmin " + xMin);
                E.remove(xMin);

                    //checking id X exists in data points if no  return  close elements
                    for (int j = 0; j < S.getRow(xMin).length; j++) {
                        if (S.getEntry(xMin, j) > beta) {
                            l.add(j);
                            dataPoints.add(j);
                            E.remove(j);

                        }

                    }
                    clusters.put(xMin, l);

here is the Minx function

public double MinX(ConcurrentMap<Integer, Double> e, HashSet<Integer> h) {
        double tmpMin = 1;
        //int Xmin = 0;
        for (int k = 0; k < e.size(); k++) {
            if ((e.get(k) < tmpMin) && (!h.contains(e.get(k)))) {
                tmpMin = e.get(k);
            }

        }

        return tmpMin;
    };

Now this is giving me null pointer exception ```

at MinX()

Her sincerly
  • 373
  • 1
  • 13
  • 1
    I see `while (E.size() ...)` but I don't see any declaration of `E` — also, modifying a collection (like Map) while looping over it leads to undefined behavior. The only safe way to do it is by using an Iterator, like `youmap.keySet().iterator()`, and remove via the iterator, but since your modifying the _Map_, not the _keySet_ that's still iffy. You should look into using a `ConcurrentMap` – Stephen P Apr 19 '19 at 20:12
  • 1
    https://stackoverflow.com/questions/1066589/iterate-through-a-hashmap take a look at this – James Russo Apr 19 '19 at 20:12
  • I edited the code and added how E is declared, note that it is not empty and it contains values as I showed in the post – Her sincerly Apr 19 '19 at 20:17
  • I tried using concurrentMap , but still the same issue NPE – Her sincerly Apr 19 '19 at 20:46
  • _Waitaminute!_ you're getting a `NullPointerException`? You didn't say that _at all_ in your question -- you only said _"retrieves the first min, **and stops**"_. Look at the traceback; on what line is the NPE happening? James' comment and link are still relevant, and also see https://stackoverflow.com/q/3988788/17300 – Stephen P Apr 19 '19 at 21:40
  • I'm going to edit the question, with my last modification, now I'm going back to the same issue. E is still not empty yet it stops I don't know why – Her sincerly Apr 20 '19 at 09:10
  • the issue is that it goes and find Minx, and construct the clusters, yet E is still not empty , there are other points left – Her sincerly Apr 20 '19 at 09:13

1 Answers1

0

The issue is from public double MinX(ConcurrentMap<Integer, Double> e, HashSet<Integer> h) { double tmpMin = 1; for (int k = 0; k < e.size(); k++) { if ((e.get(k) < tmpMin) && (!h.contains(e.get(k)))) { tmpMin = e.get(k); } } return tmpMin; };

The last element of the e ConcurrentMap is found null. Hence, it should be replaced as:




    public double MinX(ConcurrentMap<Integer, Double> e, HashSet<Integer> h) {
            double tmpMin = 1;
    for (Integer k : e.keySet()) {
                System.out.println("E inside fo"+e.get(k));
                if (e.get(k) < tmpMin && !h.contains(e.get(k)))
    {

                    tmpMin = e.get(k);
                }

        }System.out.println("min entropy is: " + tmpMin);

            return tmpMin;}



Her sincerly
  • 373
  • 1
  • 13