Error changing label - Javafx

Asked

Viewed 63 times

-1

I’m trying to change a label lbl1 by the FXML controller builder method.

public class TestController extends Application {

@FXML
private Label lbl1;

public TestController()

{

lbl1.setText("Teste");

}

@Override
public void start(Stage primaryStage) 

{                       
}

public static void main(String[] args) 

{
    launch(args);
}
}

When trying to change the label text, the following error occurs:

javafx.fxml.Loadexception: /D:/Users/Gabriel%20August/eclipse-Workspace/Dialog/bin/FXML/Test.fxml:10

at javafx.fxml.Fxmlloader.constructLoadException(Fxmlloader.java:2601) at javafx.fxml.Fxmlloader.loadImpl(Fxmlloader.java:2579) at javafx.fxml.Fxmlloader.loadImpl(Fxmlloader.java:2441) at javafx.fxml.Fxmlloader.load(Fxmlloader.java:2409) at application.test.start(Login.java:47) at com.sun.javafx.application.Launcherimpl.lambda$launchApplication1$166(Launcherimpl.java:863) at com.sun.javafx.application.Platformimpl.lambda$runAndWait$179(Platformimpl.java:326) at com.sun.javafx.application.Platformimpl.lambda$null$177(Platformimpl.java:295) at java.security.Accesscontroller.doPrivileged(Native Method) at com.sun.javafx.application.Platformimpl.lambda$runLater$178(Platformimpl.java:294) at com.sun.Glass.ui.Invokelaterdispatcher$Future.run(Invokelaterdispatcher.java:95) at com.sun.Glass.ui.win.WinApplication. _runLoop(Native Method) at com.sun.Glass.ui.win.WinApplication.lambda$null$152(Winapplication.java:177) at java.lang.Thread.run(Unknown Source) Caused by:java.lang.Nullpointerexception at application.TestController.(Testcontroller.java:198) at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Unknown Source) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at sun.reflect.Misc.ReflectUtil.newInstance(Unknown Source) at javafx.fxml.Fxmlloader$Valueelement.processAttribute(Fxmlloader.java:927) at javafx.fxml.Fxmlloader$Instancedeclarationelement.processAttribute(Fxmlloader.java:971) at javafx.fxml.Fxmlloader$Element.processStartElement(Fxmlloader.java:220) at javafx.fxml.Fxmlloader$Valueelement.processStartElement(Fxmlloader.java:744) javafx.fxml.Fxmlloader.processStartElement(Fxmlloader.java:2707) javafx.fxml.Fxmlloader.loadImpl(Fxmlloader.java:2527) ... 12 more

The controller is properly configured to FXML. The error occurs when trying to change the label text contained in FXML.

1 answer

0

Try using the MVC standard, where you separate your view from the controller.

From what I’ve seen, you’re using a controller to start the application, which you don’t. you must use a Main (extends Application) only to start your main screen.

And in turn this main screen (view(FXML)) has to have a controller that will control everything from your screen.

being like this:

Main

public class Main extends Application {
    carregar sua FXML principal...
}

in the main view FXML you arrow the fx:controller="..."

This controller will have the reference if your Label:

@FXML
private Label lbl1;

so you can set the text you want and following the patterns...

Ah, and implement Initializable in your control class to change the label:

public class SUA_CONTROLLER implements Initializable{

    @FXML
    private Label lbl1;;

    public void initialize(URL location, ResourceBundle resources) {
        lbl1.setText(....);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.