Bill Tudor
Bill Tudor

Reputation: 97

How to call this class?

I have my main class which invokes a custom JFrame class called MainFrame

public class App {

public static MainFrame mf;

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
             MainFrame mf = new MainFrame(Workers); //jFrame
        }   
    }); 
}

public static void foo(String s){ //invoked by another class
    mf.validate();

}       
}

A method calling for mf outside outside of run() returns a null value. How can I invoke a method inside of MainFrame?

Upvotes: 3

Views: 374

Answers (3)

user3584405
user3584405

Reputation:

Declare the class outside of the method. Like this:

MainFrame mf;

// following actions...

Upvotes: 1

Lynx
Lynx

Reputation: 506

You just have to declare your class outside of the method scope in order to use it later.

MainFrame mf;

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        mf  = new MainFrame(Workers); //jFrame
    }   
});

It is happening because of a thing called scope. Your variable is alive only in the brackets where it was declared. After that the garbage collector automatically removes it.

Upvotes: 3

Sajad Karuthedath
Sajad Karuthedath

Reputation: 15847

you have already declared MainFrame mf; outside

so just replace this statement

MainFrame mf = new MainFrame(Workers);

with

 mf  = new MainFrame(Workers); //jFrame

inorder to access the object outside of run()

Upvotes: 3

Related Questions