• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException on static singleton?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following (basically) in a rather large application, It flags an error (it used to throw an exception).
Any clue as to why this might happen?

A couple of notes:
  • There is code in the SingletonA constructor
  • Once it re-initializes the reference everything works
  • originally singletonA was final, but obviously I can't fix it in that case




  •  
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What do you mean by "flag an error"? Are you saying that it doesn't compile?

    Henry
     
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok....
    the problem is that the statics in SingletonB get executed before the statics in SingletonA .. That means that all static variables in SingletonA are still null, that is why you get null for SingletonA in the SingletonB class.

    Here is how you should implement it.

     
    Sebastian Janisch
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh, one more thing

    the reason why it does not throw a NPE when you added


    is because by then all statics in all classes have been executed and the getInstance method returns the instance instead of null. Thats why you don't run into a NPE when executing singleton.stuff()
     
    Gordon Oliver
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sebastian Janisch wrote:Ok....
    the problem is that the statics in SingletonB get executed before the statics in SingletonA .. That means that all static variables in SingletonA are still null, that is why you get null for SingletonA in the SingletonB class.

    Here is how you should implement it.
    ...


    While that should work fine, I am more than a bit disturbed by the fact that Java seems to not guarantee that static variables are initialized before static functions can be called. I.e. The class initialization seems to be failing here. This would also imply that you can never count on static variables to be initialized when in a static method.

    This implies that the static method is called before completion of the <clinit> function. I would think that is _bad_
     
    Sebastian Janisch
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well think about it this way ...

    In a project with 56837465784365783 classes, how long would it take to load every single class and initialize it's static members ??

    That is why the static block and member initialization will run when the class is first needed. Performancewise this makes perfect sense.

    You just have to watch out for those little traps in your code that's all.
     
    Sebastian Janisch
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hold on I think I've made a giant mistake here ...

    since you declare

    private static SingletonA singletonA = SingletonA.getInstance();

    you touch the SingletonA class which means the statics should run first before the first method can be executed...

    maybe that is some quirk in java that when you are in a static initialization and execute something on a class that has not been initialized yet it won't get initialized before the other initialization has finished ... wow that sentence is crazy
     
    Ranch Hand
    Posts: 101
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Sebastian Janisch wrote:the problem is that the statics in SingletonB get executed before the statics in SingletonA ...

    You're wrong.
    Both singletons are created at class load time, meaning that you can run into situation that some (in this case SingletonA) would be null.
    The example you've posted is just a "different" approach of creating singleton (lazy).

    Oliver:
    Problematic line: 5 (missing return type).


    Regards,
    Rok
     
    Sebastian Janisch
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rok Štelcer wrote:
    Both singletons are created at class load time, meaning that you can run into situation that some (in this case SingletonA) would be null.


    Isn't that what I said ??

    Rok Štelcer wrote:
    Oliver:
    Problematic line: 5 (missing return type).
    Rok


    I don't think that is the problem here ... That was a typo ... by flagging an error i think he meant that the program jumps into the if(singletonA == null) statement and prints that the variable is null, meaning the code compiles.
     
    Gordon Oliver
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rok Štelcer wrote:Hi,

    Sebastian Janisch wrote:the problem is that the statics in SingletonB get executed before the statics in SingletonA ...

    You're wrong.
    Both singletons are created at class load time, meaning that you can run into situation that some (in this case SingletonA) would be null.
    The example you've posted is just a "different" approach of creating singleton (lazy).

    Oliver:
    Problematic line: 5 (missing return type).


    Regards,
    Rok


    Well, yes, that was a typo. In the real application, the code executes, but the value initializes to null in the statics.
    It really shouldn't happen as far as I can tell, but it certainly does.

    BTW: if it is a 'quirk' in the JDK, i'd call it more of a serious bug than a 'quirk'
     
    Sebastian Janisch
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I ran this test:


    and it prints

    static class 1 initializes...
    static method in Static class 1 executes
    static class 2 initializes...
    static method in Static class 2 executes

    just the way I would expect it.

    Are you sure there isn't another flaw in your code?


    edit: ___ i also tried to call method() in StaticClass2 directly from the static block and when declaring a static variable and all works fine. So something has to be messed up in your code.

    another edit: ___ i also ran your code and it does not print the error nor does it throw a NPE ....
     
    Gordon Oliver
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sebastian Janisch wrote:I ran this test:
    and it prints

    static class 1 initializes...
    static method in Static class 1 executes
    static class 2 initializes...
    static method in Static class 2 executes

    just the way I would expect it.

    Are you sure there isn't another flaw in your code?


    edit: ___ i also tried to call method() in StaticClass2 directly from the static block and when declaring a static variable and all works fine. So something has to be messed up in your code.

    another edit: ___ i also ran your code and it does not print the error nor does it throw a NPE ....


    Yup. That is what I would expect as well.
    I can't really post all of the code here, but the error was a null pointer exception on a final static reference to a singleton, and putting that little piece of code (checking for null and re-assigning) fixed the problem (mostly). It only occurs on one out of maybe a hundred or so similar references sprinkled throughout the code, so it wouldn't surprise me if it were a race condition.

    Sorry, I probably should have put that in the first posting (about it only failing in one of many cases).
     
    Rok Štelcer
    Ranch Hand
    Posts: 101
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You should check the following link: Strange problem - why Singleton.getInstance() returns null?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!