Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FocusManager NullPointerException #326

Closed
fgnm opened this issue Jun 30, 2020 · 5 comments
Closed

FocusManager NullPointerException #326

fgnm opened this issue Jun 30, 2020 · 5 comments
Labels

Comments

@fgnm
Copy link
Contributor

fgnm commented Jun 30, 2020

I have a NullPointerException when a VisImageButton is clicked and Spinner is focused. This is the stack trace:

Exception in thread "main" java.lang.RuntimeException: Actor: VisImageButton
|  Image: tool-transform
	at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:203)
	at com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:159)
	at com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:283)
	at com.badlogic.gdx.InputMultiplexer.touchDown(InputMultiplexer.java:113)
	at com.badlogic.gdx.InputEventQueue.drain(InputEventQueue.java:83)
	at com.badlogic.gdx.backends.lwjgl3.DefaultLwjgl3Input.update(DefaultLwjgl3Input.java:208)
	at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:394)
	at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:138)
	at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:114)
	at games.rednblack.editor.Main.main(Main.java:40)
Caused by: java.lang.NullPointerException
	at com.kotcrab.vis.ui.FocusManager.switchFocus(FocusManager.java:42)
	at com.kotcrab.vis.ui.widget.VisImageButton$1.touchDown(VisImageButton.java:95)
	at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:58)
	at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:189)
	... 9 more

I don't know how hard is to reproduce, because in my project the crash happens only on some Spinner, expecially when they're placed in multiple nested VisTable wrapped from a ScrollPane. Reading source code it seams that focusedWidget is null for some reason L42.
Maybe a null check could fix the problem, please let me know if I can debug better the situation.
Thanks!

@kotcrab
Copy link
Owner

kotcrab commented Jun 30, 2020

switchFocus (Stage stage, Focusable widget) is always called with this as the widget- it can't be null at that point. L95

resetFocus can set it to null but to cause this it would have to be done simultaneously by some other thread. Are you using multiple threads? FocusManager is not thread safe.

@kotcrab kotcrab added the ui label Jun 30, 2020
@kotcrab
Copy link
Owner

kotcrab commented Jun 30, 2020

There is also a chance that stage.setKeyboardFocus somehow ended up calling resetFocus. I would need a test case to debug this.

@fgnm
Copy link
Contributor Author

fgnm commented Jun 30, 2020

I've noticed that it happens only when the Spinner is placed in that particular nested position. I'll try to create a test case in order to reproduce.
Anyway, I'm not using multi thread. And the crash happens always to that Spinners

@fgnm
Copy link
Contributor Author

fgnm commented Jun 30, 2020

With break points I've found the problem! And maybe nested stuff doesn't really matters. The problem is in stage.setKeyboardFocus.

  1. I have a ChangeListener attached to the Spinner and during the stage.setKeyboardFocus(null) this listener is triggered
  2. In changed method one of my VisTextField is set to disabled state, so this trigger resetFocus (Stage stage, Actor caller) and focusedWidget becomes null.
  3. We have the crash.

Now,I can try to avoid that the ChangeListener triggers setDisabled on the other widgets and fix on my code, but even for further problems would be better to change FocusManager in order to prevent this kind of conditions. Please let me know

fgnm added a commit to fgnm/vis-ui that referenced this issue Jun 30, 2020
@kotcrab
Copy link
Owner

kotcrab commented Jun 30, 2020

To add to this, the issue is triggered by the widget the focus is being switched to. It will call switchFocus with self, set the focusWidget to self and do stage.setKeyboardFocus(null) which will then trigger the change listeners on spinner (only if TextFieldEventPolicy.ON_FOCUS_LOST is used) and then in that change listener you must call some method which calls resetFocus.

@kotcrab kotcrab added the bug label Jul 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants