I've a GUI Class Named as NewFrame.java
and another one is Client.java
. in Client.java, the NewFrame's object is made inside a thread. ex,
public class ClientA {
NewJFrame gui;
int x ;
public void go() {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
gui = new NewJFrame();
gui.setVisible(true);
x = 10;
}
});
System.out.println(x); // does not print 10 but it prints 0
gui.setTextboxvalue("value changed !!!");
// can'se pass value to the function, with this error occurs as,
// "java.lang.NullPointerException" at here.
}
}
here two problems occuring, there is the setter method in NewFrame class named setTextboxValue
but calling this as shown above the error occurs "java.lang.NullPointerException".
another problem is : why i can't set the variable x
inside that thread, setting this inside thread, the outside printing the variable x
's value which is 10 is not printing there instead it prints 0
EventQueue.invokeLater
invokes its methods on a separate thread, which is fired off asynchronously on the event dispatch thread after all the other tasks on the event dispatch thread have finished. In other words, that code inside the run() method is likely going to be executed after your method finishes. Therefore, the gui
variable is still going to be null, resulting in a NullPointerException
when you call gui.setTextboxvalue
; and x is still going to be 0 when you print out the value.
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.