Results 1 to 3 of 3
- 02-19-2013, 11:38 AM #1
Member
- Join Date
- Apr 2010
- Location
- london UK
- Posts
- 53
- Rep Power
- 0
Problem with a pair of singletons
I have a problem with a pair of singletons
program (simplified) :
Java Code:public static void main(String[] args){ SingletonOne other1 = SingletonOne.getInstance(); SingletonTwo other2 = SingletonTwo.getInstance(); other1.doSomething(); other2.doSomething(); } public class SingletonOne{ // singleton private static final SingletonOne unique = new SingletonOne(); private final SingletonTwo other = SingletonTwo.getInstance(); private SingletonOne(){ // private constructor } public static SingletonOne getInstance(){ return unique; } public void doSomething() { System.out.println("singleton ONE do something 1"); other.doSomething2(); } public void doSomething2() { System.out.println(" singleton ONE do something 2"); } // other methods } public class SingletonTwo{ // singleton private static final SingletonTwo unique = new SingletonTwo(); private final SingletonOne other = SingletonOne.getInstance(); private SingletonTwo(){ // private constructor } public static SingletonTwo getInstance(){ return unique; } public void doSomething() { System.out.println("singleton TWO do something 1"); other.doSomething2(); } public void doSomething2() { System.out.println("singleton TWO do something 2"); } // other methods }
singleton ONE do something 1
singleton TWO do something 2
singleton TWO do something 1
Exception in thread "main" java.lang.NullPointerException
at test.SingletonTwo.doSomething(SingletonTwo.java:21 ) (what is line 54 above)
What is happening here and what is the best way to prevent it?
- 02-19-2013, 12:00 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Problem with a pair of singletons
Your singleton(s) try to get a reference to the other singleton just before their constructor code is run. The constructor is run at class initialization time of the other constructor and just before the 'unique' reference is initialized it is asked for by the static getInstance() method. The remedy is simple: don't do other.doSomething() but take care of lazy (singleton) instantiation and do 'SingtonOne.getInstance().doSomething()' (and similar in the other singleton class).
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-19-2013, 12:11 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Problem with a pair of singletons
Add a System.out.println() into each constructor so you can identify which constructor is currently being called.
That'll show you that the 'unique' variables are null until the relevant constructor has finished...but you are accessing them in order to populate the 'other' variable.
The solution?
Don't store a local version.
It's not needed.
ETA: And once again I'd left the window open too long!Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
I Need Help With Constructing A Pair! thank you! Please Help!
By Czajkus in forum New To JavaReplies: 3Last Post: 10-01-2012, 11:49 AM -
2 pair Combination of four values.
By nandhinianand in forum New To JavaReplies: 18Last Post: 02-27-2012, 09:32 AM -
singletons
By issepower in forum New To JavaReplies: 1Last Post: 11-24-2011, 04:44 AM -
2 pair Combination of four values.
By nandhinianand in forum NetBeansReplies: 3Last Post: 09-15-2011, 08:45 AM
Bookmarks