-1

Reposting a question with more detail. The problem is a nullPointerException which occurs at clusterer.next(). Below is a simplified version of the program and am hoping you can notice where the issue is.

public class Clustering {
    private DistanceMeasure measure;  //Manhattan or Euclidean
    private ClusterMethod method;  //SingleLinkage or CompleteLinkage
    private Clusterer clusterer;

    public static void main(String[] args) {
    new Clustering().start();
    }
    Clustering() {
        measure = new Manhattan();
        method = new SingleLinkage(measure);
        clusterer = new Clusterer(method);
    }
    void start() {
        clusterer = setMethod();
        clusterer.next(); //would use the ClusterMethod to do some calculations; the nullException occurs here
    }
    Clusterer setMethod() {
        measure = new Euclidean();
        if (method.getClass().getSimpleName().equals("SingleLinkage")) {
             method = new CompleteLinkage(measure);
        }
        else method = new SingleLinkage(measure);
        return new Clusterer(method);
    }
}
public class Clusterer {
    private ClusterMethod method;

    Clusterer(ClusterMethod method) {
        this.method = method;
    }
    void next() {
        System.out.println(method.calculateDistance(0,1); //calculateDistance simply returns the 'distance' between 2 numbers.
    }
} 

The code above works perfectly if i don't use the setMethod();

Dimebag
  • 833
  • 2
  • 9
  • 29
  • 1
    Please add a stacktrace. – Jens Jun 23 '14 at 11:19
  • Exception in thread "main" java.lang.NullPointerException at finalPAD.Clusterer.getClusterStep(Clusterer.java:23) at finalPAD.Clustering.performClusterStep(Clustering.java:91) at finalPAD.Clustering.processEvents(Clustering.java:54) at finalPAD.Clustering.proceedClustering(Clustering.java:48) at finalPAD.Clustering.start(Clustering.java:42) at finalPAD.Clustering.main(Clustering.java:34) where getClusterStep() is next() in my example here – Dimebag Jun 23 '14 at 11:24
  • 3
    Post the correct code snippet: finalPAD.Clusterer.getClusterStep(Clusterer.java:23 is where the error occurs and it is not shown in your code snippet – maress Jun 23 '14 at 11:29
  • Unless you want me to post 20 classes of code, bear with me. The getClusterStep(Clusterer.java:23) line in the stacktrace in my actual program corresponds to the 'System.out.println(method.calculateDistance(0,1);' in my example here. Like I said, program works perfectly if I don't call setMethod() so i believe my way of handling the object references is wrong somewhere. – Dimebag Jun 23 '14 at 11:33
  • What happens in Clustering.performClusterStep? – wladimiiir Jun 23 '14 at 11:50

2 Answers2

2

If your comment about where the exception occurs is correct, then method is null. You should verify this by using a debugger to step to that piece of code and inspect the statement. Alternatively, add Sysouts for all involved variables.

If this is unexpected, say you're convinced that methodis set by some other part of your code, add a breakpoint or output to that other part to verify that it's actually doing what you think it's doing, and in the correct order, too.

Community
  • 1
  • 1
hiergiltdiestfu
  • 2,339
  • 2
  • 25
  • 36
0

It turns out that I'm an idiot :) I was basically checking somewhere

"SingleLinkage".equals("Single Linkage") 

which of course is always false and the method was not assigned any ClusterMethod. The code here is fine; stuff was going wrong in another place.

Dimebag
  • 833
  • 2
  • 9
  • 29