-1

I have an interface Fruit which is implemented in Apple and Banana. In my main class, Fruit is called as following:

Fruit fruit = new Apple();

How can I retrieve a String of "Apple"? Using

fruit.getClass().getName()

OR

fruit.getClass().getSimpleName()

gives a java.lang.NullPointerException

I am trying to avoid making a function in Apple and Banana e.g. getClassName() .

EDIT: Adding more details/methods, since my simplified question above doesn't seem to point to the problem. Adding more than the below would mean copying the whole code. Again, the below is a simplification.

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; clusterMethod which is saved inside Clusterer class when creating the new class i.e. new Clusterer(method). 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);
    }
}

Does the above seem correct? The nullException occurs when using the clusterer.next() which works fine without using setMethod().

Dimebag
  • 833
  • 2
  • 9
  • 29
  • 6
    what does `fruit.getClass().getSimpleName()` return? – omu_negru Jun 23 '14 at 09:47
  • Has your `Fruit` reference been assigned to something other than an `Apple` instance before runtime testing for class name? – Mena Jun 23 '14 at 09:48
  • I get a java.lang.NullPointerException when using getName() or getSimpleName() – Dimebag Jun 23 '14 at 09:49
  • 2
    @Dimebag: Not from `fruit.getClass().getSimpleName()` you don't. Please post a short but complete program demonstrating the problem, and please make your question more specific than "doesn't seem to work". – Jon Skeet Jun 23 '14 at 09:50
  • @Dimebag a totally different problem. Your fruit has nto been instantiated corretly – NimChimpsky Jun 23 '14 at 09:50
  • you need to call it with `Fruit.class.getClass().getSimpleName()` – Mzf Jun 23 '14 at 09:50
  • @TheLostMind fix the comment – Mzf Jun 23 '14 at 09:52
  • @Mzf: That kind of defeats (what I believe is) the purpose of checking which type of `Fruit` the reference is pointing to. – Keppil Jun 23 '14 at 09:54

1 Answers1

4

Response from comments: your fruit.getClass()... call throws a NullPointerException.

This means that your fruit variable is assigned a null value at the time of testing for class name, which is likely to take place after assigning it with a new instance of Apple.

Invoking a method on a null Object will throw a NullPointerException.

Note

As an afterthought, if the NullPointerException is thrown before reaching the getClass() method invocation, then you have a different issue in some other part of your code that throws a NullPointerException and is not related with runtime class name checking at all.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • It seems to be the second option. The getSimpleName() does return the name but something else in my code doesn't seem to work after doing that. I'll return after some more 'playing'. – Dimebag Jun 23 '14 at 10:03
  • Modified the original question with more concise code that I'm using. Please take a look at it. – Dimebag Jun 23 '14 at 10:38
  • @Dimebag you haven't posted your `Clusterer` class, namely the code in the `next` method, which is what's causing the `NPE` according to your own comment. I suggest asking a new question. – Mena Jun 23 '14 at 10:45
  • Reposted here http://stackoverflow.com/questions/24364554/nullpointerexception-guess-when-using-getclass-getname – Dimebag Jun 23 '14 at 11:44