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();