Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application Design a Better App Separating the Layout

Rokas Mazeika
Rokas Mazeika
4,243 Points

i have an error and i cant figure what's bad

Exception in Application start method
Exception in thread "main" 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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Gamer/IdeaProjects/yo/out/production/yo/sample/sample.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.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Unable to coerce  fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">  to class javafx.scene.Node.
    at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:496)
    at javafx.fxml.FXMLLoader$ValueElement.getListValue(FXMLLoader.java:807)
    at javafx.fxml.FXMLLoader$ValueElement.processCharacters(FXMLLoader.java:879)
    at javafx.fxml.FXMLLoader.processCharacters(FXMLLoader.java:2836)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2537)
    ... 17 more

4 Answers

Without seeing the code you have written, I cannot tell exactly what the issue is but based on the call stack you provided it appears that the error in your code is on line 23 in the Main.java file of your project.

Rokas Mazeika
Rokas Mazeika
4,243 Points

my main.java

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.control.*;

import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.Objects;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        /*Group root = new Group();
        Text txt = new Text("Sup?");
        Button btn = new Button();
        TextField fld = new TextField();
        GridPane grid = new GridPane();
        VBox box = new VBox();
        Label label = new Label("Name:");

        btn.setText("Say sup!");
        btn.setOnAction(evt -> {
            System.out.printf("Sup %s! %n", fld.getText());
        });
        grid.add(fld, 1, 0);
        grid.add(label, 0, 0);
        grid.add(btn, 1, 1);
        grid.setHgap(20);
        grid.setGridLinesVisible(true);
        box.getChildren().addAll(txt, grid);
        root.getChildren().add(box);*/
        primaryStage.setTitle("Sup program");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <Text text="Sup"
          GridPane.halignment="CENTER"
          GridPane.rowIndex="2"
          GridPane.columnSpan="0"/>
    <Button text="Say sup"
            GridPane.columnSpan="1"
            GridPane.rowIndex="2"/>
    <Label text="Name:"
           GridPane.columnSpan="0"
           GridPane.rowIndex="1" />
    <TextField fx:id = "name"
               GridPane.columnSpan="1"
               GridPane.rowIndex="1"
               GridPane.halignment="RIGHT"/>
</GridPane>

my sample.fxml

Sean Hayes
PLUS
Sean Hayes
Courses Plus Student 8,106 Points

I'm having the same issue. Did you ever find a solution?

Gustáv Foltin
Gustáv Foltin
16,402 Points

They can remove this JavaFX part of intermediate course, unable to do it. A lot of erros appears, cant run app.