JAVAFX with css implementation

0

I have this Java code from javafx

public class FormularioGeneral extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {            
        TabPane tabPanel = new TabPane();
        Tab tab = new Tab();
        Tab tab2 = new Tab();
        Scene scene= new Scene (tabPanel, 1000,1000);

        Label usuario= new Label("usuario");
        TextField usuarioT = new TextField();
        Label password = new Label("password");
        PasswordField passwordT = new PasswordField();
        Label sexo = new Label("sexo");
        ToggleGroup group = new ToggleGroup();
        RadioButton r1 = new RadioButton();
        RadioButton r2 = new RadioButton();
        r1.setToggleGroup(group);
        r1.setText("hombre");
        r2.setToggleGroup(group);
        r2.setText("mujer");
        Label hobby = new Label("Hobby");
        ComboBox hobbyCombo = new ComboBox();
        hobbyCombo.getItems().addAll("futbol","baloncesto","escalada","alpinismo","sofing","OTROS...");
        Label descripcion = new Label("Descripcion");
        TextArea areaDescripcion = new TextArea();
        Label formacion = new Label("Formacion");
        CheckBox fp1 = new CheckBox();
        fp1.setText("Fp1");
        CheckBox fp2 = new CheckBox();
        fp2.setText("Fp2");
        Button b1  = new Button();
        b1.setText("ENVIAR");
        b1.setId("boton1");
        Button b2 = new Button();
        b2.setText("CANCELAR");
        b2.setId("boton2");

        HBox otro = new HBox();
        otro.setSpacing(10);
        otro.getChildren().addAll(b1,b2);

        b2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                usuarioT.setText("");
                passwordT.setText("");
                areaDescripcion.setText("");
                fp1.setSelected(false);
                fp2.setSelected(false);
                r1.setSelected(false);
                r2.setSelected(false);
                hobbyCombo.setValue(null);
            }
        });

        VBox root = new VBox();
        root.setSpacing(10);

        root.setPadding(new Insets (10,200,20,100));
        root.getChildren().addAll(usuario,usuarioT,password,passwordT,sexo,r1,r2
                                  ,hobby,hobbyCombo,descripcion,areaDescripcion,
                                  formacion,fp1,fp2,otro);

        tabPanel.getTabs().addAll(tab,tab2);
        tab.setText("Formulario");
        tab2.setText("Formulario vacio ");
        tab.setContent(root);


        primaryStage.setScene(scene);
        primaryStage.show();
        String css = this.getClass().getResource("css.css").toExternalForm();
        scene.getStylesheets().add(css);
    }

    public static void main(String[] args) {
        launch(args);
    }

}

And the following CSS code

#boton1 { 
    -fx-text-fill: red; 
}

The problem is that at the time of execution I am giving the following error:

  
    

Exception in Application start method     java.lang.reflect.InvocationTargetException         at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)         at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)         at java.lang.reflect.Method.invoke (Method.java:498)         at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs (LauncherImpl.java:389)         at com.sun.javafx.application.LauncherImpl.launchApplication (LauncherImpl.java:328)         at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)         at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)         at java.lang.reflect.Method.invoke (Method.java:498)         at sun.launcher.LauncherHelper $ FXHelper.main (LauncherHelper.java:767)     Caused by: java.lang.RuntimeException: Exception in Application start method         at com.sun.javafx.application.LauncherImpl.launchApplication1 (LauncherImpl.java:917)         at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 154 (LauncherImpl.java:182)         at java.lang.Thread.run (Thread.java:748)     Caused by: java.lang.NullPointerException         at fxengeneral.FormularioGeneral.start (FormularioGeneral.java:103)         at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 161 (LauncherImpl.java:863)         at com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 174 (PlatformImpl.java:326)         at com.sun.javafx.application.PlatformImpl.lambda $ null $ 172 (PlatformImpl.java:295)         at java.security.AccessController.doPrivileged (Native Method)         at com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 173 (PlatformImpl.java:294)         at com.sun.glass.ui.InvokeLaterDispatcher $ Future.run (InvokeLaterDispatcher.java:95)         at com.sun.glass.ui.gtk.GtkApplication._runLoop (Native Method)         at com.sun.glass.ui.gtk.GtkApplication.lambda $ null $ 48 (GtkApplication.java:139)         ... 1 more     Exception running application fxengeneral.FormularioGeneral     / media / verodaht / 2cdb6e9f-d1e4-4a65-a5c7-e79e268bb88e / 2DAM / Interface development / projects / FXEnGeneral / nbproject / build-impl.xml: 1052: The following error occurred while executing this line:     / media / verodaht / 2cdb6e9f-d1e4-4a65-a5c7-e79e268bb88e / 2DAM / Development of interfaces / projects / FXEnGeneral / nbproject / build-impl.xml: 806: Java returned: 1     BUILD FAILED (total time: 0 seconds)

  

The problem is giving me when I add the CSS

I'm using netbeans on ubuntu 18

    
asked by Sergio Alcalde 14.12.2018 в 12:55
source

1 answer

1

You have added an extra level of abstraction when calling the method. The "reflection" layer adjusts any exception in an exception type "InvocationTargetException", which allows you to determine the difference between an exception actually caused by a failure in the reflection call (maybe your argument list was not valid, for example ) and a flaw in the method called.

Simply use the getCause () method in the "InvocationTargetException" exception to return the original exception.

Do the following: Remove the thwors Exception from your function so it looks like this:

public void start(Stage primaryStage){
    try{
        ... aquí tu código
    }catch(Exception e){
        e.printStackTrace();
    }
}
    
answered by 14.12.2018 в 13:10