简体   繁体   中英

java an object that is set in EventQueue.invokeLater is null

I have a Class MyClass, that when it runs it initialize a GUI awt frame (" Launch.java "), by calling the GUI's main method. in this main method i have EventQueue.invokeLater whose runnable actually does all the initialization. One part of the initialization is to set an object called " controller ".

After calling the GUI's main method from MyClass, I want to getController() the controller, but it returns null, and I don't know why.

when i debugged it i put a breakpoint in Launch.java on the line where controller is created ( controller = new Controller(textArea) ), and i noticed that it is not null then, but back in MyClass it is null when I call Launcher.getController() .

What am I doing wrong?? Thanks!

MyClass:

public class MyClass{

private static boolean isInitialized = false;
private Controller control;

public static void main(String[] args){

    if (!isInitialized)
    {
        Launch.main(new String[1]);
        control = Launch.getControl(); //here control is null!
        isInitialized = true;
    }
    //irrelevant code here      
}
}}

Launch.java:

public class Launch {

private JFrame frame;
private JTextField inputField;
DefaultListModel model;
private static Controller controller;
JSpinner spinner;

public static Controller getControl()
{
    return controller;
}


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Launch window = new Launch();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Launch() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
        //some code here... (here controller is null)

        controller = new Controller(textArea);

        //some code here...  (here controller is not null - during debug)
    }
}

There are several problems with your code. The most glaring is that you invoke main() and then getControl() synchronously on the servlet thread. However the code that creates the controller is invoked at some indeterminate future time, assuming the existence of an AWT dispatcher thread and system event queue. At the time you invoke getControl() the Launch object has most likely not been instantiated.

Assuming the rest of your code does what you want, what you need to do is move the invocation of getControl() to just before you use its return value.

I'm answering only the question you asked... however the design of your app (launching an AWT app from a servlet, the missing AWT event loop code, etc) looks questionable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM