Issue
I am writing a javafx program and I need the panel to update at a constant rate. Right now it is set to update every second. But I got this error, which is usually (but not always) followed by a glitch in the panel when the whole scene becomes distorted (it like mirrors in on itself in a weird choppy x pattern. hard to explain).
Full error: (java:22494): Gdk-WARNING **: 18:38:59.118: XSetErrorHandler() called with a GDK error trap pushed. Don't do that.
This is the code I have for the timer:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
String x = txtDisplay.getText();
txtDisplay.setText(x.substring(1, x.length()) + x.substring(0, 1));
} catch (NullPointerException e) {
System.out.println("Error.");
}
}
});
}
}, 0, 500);
I think the issue is with the above block, like maybe I am breaking some fundamental swing rule. My other idea is that it has something with two methods editing the same text area at the same time, because I have other methods setting the text area's text.
I would be happy with either a solution to the error or a better way of executing the above method. Just needs to run every second without crashing.
Thanks.
EDIT: A new developement, I am now consistently getting a "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" but the stacktrace does not reference any locations that are in my code.
Solution
This looks like is is related to JDK-8156779 (see also JDK-8211305). I.e. there are some issues with JDK 8, 9 and 11 and GTK 3.
For me (on Linux Mint 20.1 and Open JDK 11, Bellsoft 11.0.10.fx-librca) the solution was to force GTK 2 with -Djdk.gtk.version=2
Others have solved it by upgrading to JDK 12 or higher.
Answered By - Per
Answer Checked By - Katrina (JavaFixing Volunteer)