java - nullPointerException, guess when using getClass().getName() -
reposting question more detail. problem nullpointerexception occurs @ clusterer.next(). below simplified version of programme , hoping can notice 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 utilize clustermethod calculations; nullexception occurs here } clusterer setmethod() { measure = new euclidean(); if (method.getclass().getsimplename().equals("singlelinkage")) { method = new completelinkage(measure); } else method = new singlelinkage(measure); homecoming 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 returns 'distance' between 2 numbers. } } the code above works if don't utilize setmethod();
if comment exception occurs correct, method null. should verify using debugger step piece of code , inspect statement. alternatively, add together sysouts involved variables.
if unexpected, you're convinced methodis set other part of code, add together breakpoint or output other part verify it's doing think it's doing, , in right order, too.
java classname
No comments:
Post a Comment