-2

There's the Main class and 2 example classes. One extends HashMap.

The Main:

public class Main {
    public static void main(String[] args) {
        System.out.println("Positive:");
        PositiveExample positiveExample = new PositiveExample();
        positiveExample.printThis();
        System.out.println("Negative:");
        NegativeExample negativeExample = new NegativeExample();
        negativeExample.printThis();
    }
}

The standard one:

public class PositiveExample {
    void printThis() {
        System.out.println(this);
        System.out.println(this == null);
    }
}

And the HashMap based one.

import java.util.HashMap;

public class NegativeExample extends HashMap {
    void printThis() {
        System.out.println(this);
        System.out.println(this == null);
    }
}

Now take a look at the console output:

Positive:
PositiveExample@2a139a55
false
Negative:
{}
false

And notice the empty {}. As opposed to PositiveExample@2a139a55 from the standard class' output.

The HashMap based class output says this is not null, but that's how it behaves, doesn't it. Check it for yourselfs.

I'm on Java build 1.8.0_60-b27, Ubuntu 14.04 64 bit.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • Hard to fix when you're not posting any code. – Tunaki Sep 26 '15 at 11:34
  • Without your posting pertinent code, I'm not sure how you expect us to understand your problem or your code. Please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. – Hovercraft Full Of Eels Sep 26 '15 at 11:34
  • Well, the problem doesn't demand any more code than I've already posted, to my understanding. The question is WHY this returns null. Simple. It shouldn't, right? –  Sep 26 '15 at 11:36
  • I'm voting not to close it. If you don't have the answer, please don't interrupt. Thanks. –  Sep 26 '15 at 11:38
  • 1
    There is nothing to fix in your question. You are saying "I have a problem, can you fix it?" What do you expect of us? – Tunaki Sep 26 '15 at 11:38
  • Btw, you are basically saying that `this == null`, which would be quite bizarre to begin with. If this were true, a NPE would have been thrown. – Tunaki Sep 26 '15 at 11:40
  • I can give you all the bugs I have. If only I knew where to look for them. –  Sep 26 '15 at 11:41
  • NPE is thrown later, when I operate on the Other object. –  Sep 26 '15 at 11:41
  • 1
    This is where creating a [mcve] can help. It forces you to minimize your code until you have the smallest code possible that reproduces your error. It's a great tool, not just for us, the helpers here, but also for you, the questioner since it allows you to expose your bug in all its nakedness. I again urge you to try to create one. – Hovercraft Full Of Eels Sep 26 '15 at 11:42
  • 1+ for the edit to your question. Please see the edit to my answer. Your output is exactly as expected, and also as expected, `this` is **never** null. The problem is completely due to your misunderstanding of what the `toString()` method does, and how it is inherited. – Hovercraft Full Of Eels Sep 26 '15 at 13:11
  • 1
    *"The HashMap based class output says this is not null, but that's how it behaves, doesn't it. "* This makes no sense to me. That output shows that the HashMap is empty. Why do you think that `this` is `null` or behaves like that? – Tom Sep 26 '15 at 13:14
  • 1
    Btw you can easily test what you'll get if you really try to print `null`: you would get `"null"`, not `{}`. – Tom Sep 26 '15 at 13:22
  • Thanks for your lesson Hovercraft. My thinking's much better now. And thank you Tom for you null trick. That's the kind of hint worth its weight in gold. –  Sep 26 '15 at 13:37

1 Answers1

3

Do you know any examples of this in a class returning null?

No, this cannot happen. Period. You have a bug somewhere in code that is confusing you into thinking that this is null, but it's not. If you feel otherwise, then you will want to post your pertinent code to prove that your assumption is correct.

Edit: I just found a duplicate question that goes into further details.

Just a general question without any code, as it's not needed.

You're correct, to answer the direct question in your edited post above, no code is needed. However if you want to find the real problem in your code, the one that is giving you the erroneous idea that this is null, then as I noted in comments, you'll want to create and post your Minimal, Complete, and Verifiable example.


Edit 2
Thank you for updating your code. Your console output is exactly as one would expect:

Positive:                       
pkg.PositiveExample@659e0bfd  // this is the default toString of a class that has not overridden toString
false                         // as expected, this is NOT null
Negative:
{}                            // this is the toString returned from the parent class, HashMap, specifically an EMPTY HashMap
false                         // as expected, this is NOT null

The problem has all to do with your understanding (or misunderstanding) of the toString() method. If you don't override the method or have a super class that overrides it, it defaults to the base toString() method found in the Object class and then it returns the class name and the object's hashCode. If you do override it or have a parent class that overrides, it returns whatever it is told to return. Here HashMap overrides, and returns the contents of the Map, here nothing.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373