Avatar of Javin007
Javin007Flag 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
Avatar of Javin007
Javin007
Flag of United States of America image

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
Avatar of mccarl
mccarl
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Javin007
Javin007
Flag of United States of America image

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.
Avatar of mccarl
mccarl
Flag of Australia image

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.
Avatar of Javin007
Javin007
Flag of United States of America image

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!
Avatar of mccarl
mccarl
Flag of Australia image

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.
Avatar of Javin007
Javin007
Flag of United States of America image

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!
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo