0

I am working with JavaFX. I use a Scene Builder . It makes a loader and Controller class. I try to change a text area value from another thread.

   @Override
    public void run() {
        Platform.runLater(() ->  load_text.setText("Hello World"));
    }

And I make a Thread like :

       Controller loader= new Controller();
       Thread load=new Thread(loader);
       load.start();

On an thread running it cause an :

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at Loader.Controller.lambda$run$0(Controller.java:39)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)

What I am do wrong ? . I am using a Platform.runLater(() as recommended. It is a JVM bug or not?

All Code is here : https://pastebin.com/bk6JbQ8v

  • 2
    [mcve] please . – kleopatra Feb 13 '20 at 23:32
  • Why create a thread? (Normally you wouldn't need to do that). Why create a new instance of your `Controller` class in the `initialize` method? (The `FXMLLoader` will already create a `Controller` instance. Creating another in `initialize` will not initialize members in the new instance, hence your null pointer exception). To pass the existing loader to the new thread, write `new Thread(this);`, not `new Thread(loader);`. Best to have all code for a minimal example inside your question (see kleopatra's link), rather than linked to an offsite resource such as pastebin. – jewelsea Feb 14 '20 at 01:11
  • Also I advise following Java naming conventions, [package names should be all lowercase](https://google.github.io/styleguide/javaguide.html#s5.2-specific-identifier-names). – jewelsea Feb 14 '20 at 01:14

0 Answers0