Avatar of Javin007
Javin007
Flag for United States of America asked on

Why are my objects un-initializing?

So I have a fairly simple class (the "events" are just extensions off of the listeners on the parent window object):

package com.peculiarhabit.application;

import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

import com.peculiarhabit.debug.Debug;
import com.peculiarhabit.gui.Window;
import com.peculiarhabit.input.KeyWatcher;

public class MainWindow extends Window {
	public static class Builder extends Window.Builder 	{	
		@Override public MainWindow build() {
			return new MainWindow(this);	
		}	
	}


	protected MainWindow(Builder b)  {
		super(b);	
	}


	protected KeyWatcher kw = null;
	
	@Override protected void windowInitialized() {
		kw = new KeyWatcher();
		Debug.print("kw is null: " + (kw == null));
	}
	
	@Override protected void mouseClicked(MouseEvent e) 	{
		Debug.print("(click) kw is null: " + (kw ==  null));
	}
	
	@Override protected void windowClosing(WindowEvent e) {
		Debug.print("Closing");
		if (kw == null) Debug.print("Why is it null?");
	}
	
}

Open in new window



So what happens here is that when run, it pops up a window.  I click the window.  Then I click the close button in the upper right-hand corner.  This is the Debug output:

00:00:00.000.113		main	kw is null: false
00:00:01.597.709		AWT-EventQueue-0	(click) kw is null: true
00:00:06.287.066		AWT-EventQueue-0	Closing
00:00:06.287.235		AWT-EventQueue-0	Why is it null?

Open in new window


Why is the "KeyWatcher" object being dropped?  I feel like it has to be something obvious that I'm missing.
Java

Avatar of undefined
Last Comment
Javin007

8/22/2022 - Mon
Javin007

ASKER
Seems to be related to the threading for some reason.  I suspect the "main" thread is closing, and thus uninitializing all of its objects.  

While setting the object to "Final" kind of works (the object is now NOT initialized in the "init" event, but is in the further events) it's also not what I'm trying to do.  I would prefer that I be able to create/destroy the object when I want to.  

"Hanging" the main thread open (with a while loop that keeps it open as long as an "IsRunning" variable is "true") seems to get the desired result, but is that the right way to do things?  Seems a wasted thread to me.
ASKER CERTIFIED SOLUTION
mccarl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Javin007

ASKER
Indeed, it appears that the problem was in the "KeyWatcher" class itself.  The class was watching the thread that spawned it, and "cleaning up" itself when that thread closed.  It seems that the main thread "shuts down" when it's done creating the windows and other objects, and that was triggering the KeyWatcher to shut itself down.  I was under the assumption that for as long as the application was running, the "main" thread would have an "IsAlive" value of "true".  Doesn't seem to be the case.

So while I've tracked down the problems, what was your reason for using the hashcodes?  What does that tell you?  Looks interesting.
mccarl

what was your reason for using the hashcodes?  What does that tell you?
"hashcode()" is like an ID of the object. Each different object instance will have a different hashcode. So the code above was printing out the hashcode/ID of "this" which is the MainWindow object (note, it's NOT the KeyWatcher object). What I was expecting to see is at least 2 different hashcodes being printed out in those debug statements.

The thing is, even though you may have got your app working, I still think that there may be other underlying issues. Why? Because it is unlikely that anything else can change/null out your kw variable, ie. Nothing the KeyWatcher does in normal processing or as it is "shutting down" can make your kw == null.

So yeah, as I said, I still think that there is something else going on, so if can post the output with those hashcode debug statements, it would be good to double check things.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Javin007

ASKER
Ah, unfortunately I'm past that point in the code now (I don't even have the KeyWatcher class anymore).  But I'll definitely keep that nugget in mind and use the hashcodes in the future!  Thanks!
mccarl

I don't even have the KeyWatcher class anymore
Yes, but do you still have MainWindow? As I aluded to above, the issues with KeyWatcher was just a symptom of a bigger problem.

Anyway, I'll leave it up to you to decide if you want to pursue further but just so that you know, in case you see some issues in the future or that after I describe below you DO think it might still apply... What I think was the problem is that one instance of MainWindow was being created and .windowInitialized() was being called on that one, and then somehow another, different instance of MainWindow was being created later and that was what the other two methods were being called upon. Hopefully, you can see that this will cause other problems in the future if not resolved. Maybe your changes since the above code have sorted this out, but it is just something to keep in mind!! I am happy to keep this discussion open if you DO now feel like the issue may be there, after I've described it further.
Javin007

ASKER
Yes, MainWindow hasn't changed, nor has its parent, but neither dealt with the "KeyWatcher" object, but you might see something I don't.  I'll have to post again when I get back to work tomorrow. Thanks for your help!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.