Results 1 to 3 of 3
  1. #1
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default 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
    }
    If I run it I get:

    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?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default 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

  1. Replies: 3
    Last Post: 10-01-2012, 10:49 AM
  2. 2 pair Combination of four values.
    By nandhinianand in forum New To Java
    Replies: 18
    Last Post: 02-27-2012, 08:32 AM
  3. singletons
    By issepower in forum New To Java
    Replies: 1
    Last Post: 11-24-2011, 03:44 AM
  4. 2 pair Combination of four values.
    By nandhinianand in forum NetBeans
    Replies: 3
    Last Post: 09-15-2011, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •