1

I have this little piece of code here, which always will throw a NPE:

public class Test1 {
    private final static Object OBJECT = new Object() {{
        System.out.println("OBJECT.toString() = " + OBJECT.toString());
    }};
    public static void main(String[] args) { }
}

Are there ways though that OBJECT can be initialized in the instance initialization block? Or will every possible reference to OBJECT in the instance initialization block always throw a NPE?

For those Fastest-Gun-In-The-Wests, read this: No, I am not asking you to solve my bug. This is a simplified piece of code I've seen somewhere and which to my surprise does not give a null warning in either Eclipse or Netbeans, which I would expect it to give.

Konstantin V. Salikhov
  • 4,554
  • 2
  • 35
  • 48
skiwi
  • 66,971
  • 31
  • 131
  • 216
  • 3
    Why you can't just use `this` ? – Konstantin V. Salikhov Aug 20 '14 at 07:05
  • It's a bug I spotted when solving an existing issue somewhere, and neither Eclipse nor Netbeans give a null-pointer warning, which I would expect to be there. – skiwi Aug 20 '14 at 07:07
  • What about FindBugs? I think it is available as a plugin for both Eclipse and Netbeans – tucuxi Aug 20 '14 at 07:11
  • @tucuxi It is not what I am asking either. Both IDEs have a default mechanism in place to detect obvious NPE's, if this is **always** the case (what I am asking), then it should also give a warning with only base installation. – skiwi Aug 20 '14 at 07:11
  • 1
    Your field is static and any invocation on static field needn't have NULL check warning. – Maas Aug 20 '14 at 07:13
  • 1
    @Maas Why would static fields not deserve a null-check warning? – skiwi Aug 20 '14 at 07:14
  • @skiwi because static fields are generally used to point at class not at instances of that class. But I agree with you that IDE shall go back and check if the calling method is non static to flag a warning – Maas Aug 20 '14 at 07:16
  • I am pretty sure you can consider this a somewhat-obscure bug in both netbeans and eclipse (missing NPE rule for this anti-pattern). Why not file it in their bug systems? – tucuxi Aug 20 '14 at 07:18
  • @tucuxi I have done that, but I wanted some confirmation on this, such that (if I am wrong) I could either cancel the bug report or provide additional information. – skiwi Aug 20 '14 at 07:19
  • 3
    My 2c (-: :From a code analyzer point of view this field is static final and gets something assigned. That the initialize block of the objects uses the field reference would require some (extensive?) analysis. I mean the reference is only null in the constructor and ini-block, if you would access it e.g. in toString or something it wouldn't be null. – morpheus05 Aug 20 '14 at 07:29

1 Answers1

2

Double brace initialisation is just an anonymous inner class with instance initialiser. Therefore the rules governing creating objects apply.

NPE is a Runtime Exception so an IDE uses heuristics to find the culprits rather than the rigour and certainty provided by static typing, declared exceptions, and static code analysis. Here it would have to execute the code in order to find runtime issues.

What is happening is this: a static member is being initialised by assigning to it anonymous inner class. Constructor of the inner class has finished as this is the rule for instance initializers. However the reference is assigned only after initialisers have finished, which they have not by the time you try to dereference OBJECT.

Seems like a very circular explanation, but that's because what is happening is circular referencing ;)

diginoise
  • 7,352
  • 2
  • 31
  • 39