Reputation: 97
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
Reputation:
Declare the class outside of the method. Like this:
MainFrame mf;
// following actions...
Upvotes: 1
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
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