Hey guys, gonna try and explain this in an hopefully understandable way.

I've got my GUI which is called a GUI class which is a singleton so I can access it anywhere in my code and manipulate the GUI object.

I've also got a sharedMemorySection which again is a singleton which stores an ArrayList<string> allowing the system to post messages into the arraylist.

The problem starts to come to light when the messageListeners are invoked since they grab an instance of the GUI class in order to post a message but in order to create a GUI instance we need a messageListener and therefore I seem to get into an inifintive loop and a runtimestack overflow occurs.

Gui -> makes listener (but GUI is still set to NULL because it ain't finished been set up)
listener -> needs object of GUI so requests a new object since its still set to NULL

Hence a infinitive loop of getInstance calls occurs.

I'm wondering if theres a way rnd this?

//my customer logger that enables me to save messages to the arrayList

//in GUI class....

 private static  Logger diagnosticLog = DiagnosticLogger.getInstance().getLogger();
 public static Gui getInstance() {
        diagnosticLog.log(Level.INFO, "HELLlllllllllllOOOOOOOOOOOOOOOO");
        if (frame == null) {
            frame = new Gui("BTServer");
        return frame;
//Constructor of the class....
  private Gui(String name) {
        Listener listenerOnMemoryArea  = new Listener();
        SharedMemorySection.getInstance().registerListenerOntoSharedMemory (listenerOnMemoryArea);
        diagnosticLog.log(Level.INFO, "Creeating an object of GUI()");

// the Listener object...

Gui instance;
    public Listener() {
        instance = Gui.getInstance();
     * Puts the strings onto a canvas 
     * @param stringList - the list of messages to be displayed to the user.
    public void printMessagesToUser ( List<String> stringList) {
        //Iterate through the list
        Iterator <String> it = stringList.listIterator ();
        while(it.hasNext ()) {
            String str = StringFormatter.stringFormatter (it.next ());
            if(str == null) {
          // instance.logThisMessage ("USER",str);
            System.out.println(" ******************" + this.toString() + " " + str);

Hope this all make sence?

9 Years
Discussion Span
Last Post by Ezzaral

Well, which one has no context without the other? Does a listener have any useful function without a GUI? What about the GUI? It seems to me the GUI can exist without a listener, but it just won't do much until it has one. So you could create the GUI and then create the listener passing a reference to the GUI.

On the other hand, if your listener is really a dispatcher, which it kind of appears to be, it can exist and function without anything to pass message along to - it just ends up trying to send a message to an empty list until receivers register.

Consider also that maybe you don't really need those classes to be explicit singletons. They may only ever have one instance in the context of your app, but that doesn't mean it has to be enforced by the class design. Singletons can cause a lot of headaches down the road if you aren't absolutely certain of their singularity.

Votes + Comments
Good explanation :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.