0

I am attempting to create multiple JavaFX applications and have had no issues until now when I attempt to add a TextField. I can have a brand new file and include a single line initializing a TextField and it will cause the exceptions. I have looked all over online and got nothing. Simply adding the TextField causes the exceptions to appear even in other, previously running,k programs. Examples below:

Error Image

The program I have created so far:

public class CH16Q04 extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        VBox masterPane = new VBox();
        HBox milesPane = new HBox();
        HBox kiloPane = new HBox();


        javafx.scene.control.TextField milesTF = new TextField("Miles: ");
        milesTF.setAlignment(Pos.BASELINE_RIGHT);
        javafx.scene.control.TextField kiloTF = new TextField("Kilometers" );
        kiloTF.setAlignment(Pos.BASELINE_RIGHT);

        milesPane.getChildren().addAll(new Label("Miles: "), milesTF);
        kiloPane.getChildren().addAll(new Label("Kilometers: "), kiloTF);
        masterPane.getChildren().addAll(milesPane,kiloPane);
        BigDecimal milesInput = new BigDecimal(milesTF.getText());
        BigDecimal kiloInput = new BigDecimal(kiloTF.getText());

        milesTF.setOnAction(e -> {

            kiloTF.setText((milesInput.multiply(new 
            BigDecimal("1.609344")).toString()));
            kiloTF.setStyle("-fx-text-fill: red");
        });

        kiloTF.setOnAction(e -> {

            milesTF.setText(kiloInput.multiply(new 
            BigDecimal("0.621371")).toString());
            milesTF.setStyle("-fx-text-fill: red");
        });


        Scene scene = new Scene(masterPane);
        primaryStage.setTitle("Chapter 16, Question 04");
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Did you read the exception being thrown (`IllegalAccessError`)? That should be your first stop. Also, please copy/paste the exception into the question instead of linking to an image. – Zephyr Mar 13 '19 at 19:45
  • Note that this is the stacktrace of a single exception. This exception (`InvocationTargetException`) has a cause (`RuntimeException`) which in turn has it's own cause (`IllegalAccessError`). Usually the last `Caused by` part is the interesting one, since it contains the "root cause". If you put the application in a module with the appropriate `requires`, the problem should go away... – fabian Mar 13 '19 at 21:26
  • The entire line of the Illegal Access Error is: Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0xf42c050) cannot access class com.sun.javafx.scene.layout.RegionHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene.layout to unnamed module @0xf42c050 I just realized my screenshot cut it off. – Chester Taylor Mar 13 '19 at 23:15
  • FIX: I fixed the issue by adding the below to the VM options. I was under the impression if you added it to the Main application you didn't need to add it to the others, which is evidently wrong. --module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls,javafx.fxml,javafx.graphics,javafx.base – Chester Taylor Mar 13 '19 at 23:38

1 Answers1

0

You need to set the values for the TextFields before you can use them to instantiate BigDecimal objects.

 milesTF.setText("0.00");
 kiloTF.setText("0.00");

 BigDecimal milesInput = new BigDecimal(milesTF.getText());
 BigDecimal kiloInput = new BigDecimal(kiloTF.getText());
trilogy
  • 1,738
  • 15
  • 31
  • This indeed is a flaw in the program, but in this case not the root cause of the issue. Arguably the input should be read in a event handler or a listener though. This doesn't fix the error the OP posted the stacktrace for though... – fabian Mar 13 '19 at 21:32