2 Replies Latest reply: May 10, 2012 12:30 PM by 885691 RSS

    Creating an Address Book with FXML tutorial error

    923877
      i follow this tutorial:
      [http://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm#CACFEHBI|http://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm#CACFEHBI]
      until the step 7 when torws me this error:

      Exception in Application start method
      java.lang.reflect.InvocationTargetException
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
           at java.lang.reflect.Method.invoke(Method.java:601)
           at com.javafx.main.Main.launchApp(Main.java:486)
           at com.javafx.main.Main.main(Main.java:638)
      Caused by: java.lang.RuntimeException: Exception in Application start method
           at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
           at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
           at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
           at java.lang.Thread.run(Thread.java:722)
      Caused by: java.lang.ClassCastException: javafx.scene.Scene cannot be cast to javafx.scene.Parent
           at fxmltableview.FXMLTableView.start(FXMLTableView.java:25)
           at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
           at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
           at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
           at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
           at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
           at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
           ... 1 more
      Java Result: 1

      this is my fxml:
      <?xml version="1.0" encoding="UTF-8"?>

      <?import java.lang.*?>
      <?import javafx.scene.*?>
      <?import javafx.scene.control.*?>
      <?import javafx.scene.layout.*?>
      <?import javafx.geometry.Insets?>

      <Scene width="550" height="550"
      fx:controller="fxmltableview.FXMLTableViewController"
      xmlns:fx="http://javafx.com/fxml">
      <GridPane alignment="center" hgap="10" vgap="10">
      <padding>
      <Insets top="10" right="10" bottom="10" left="10"/>
      </padding>
      <Label text="Address Book" GridPane.columnIndex="0" GridPane.rowIndex="0"
      style="-fx-font: NORMAL 20 Tahoma;"/>
      <TableView fx:id="tableView" GridPane.columnIndex="0"
      GridPane.rowIndex="1">
      </TableView>
      </GridPane>
      </Scene>


      my FXMLTableViewController:

      package fxmltableview;

      import java.net.URL;
      import java.util.ResourceBundle;
      import javafx.fxml.Initializable;

      public class FXMLTableViewController implements Initializable {

      @Override
      public void initialize(URL url, ResourceBundle rb) {
      // TODO
      }
      }

      and my main class:

      package fxmltableview;

      import javafx.application.Application;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.stage.Stage;

      public class FXMLTableView extends Application {

      public static void main(String[] args) {
      Application.launch(FXMLTableView.class, args);
      }

      @Override
      public void start(Stage stage) throws Exception {
      Parent root = FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));

      stage.setScene(new Scene(root));
      stage.show();
      }
      }
      -----
      What am I doing wrong?


      -----
      javafx 2.1
      jdk1.7.0_03
        • 1. Re: Creating an Address Book with FXML tutorial error
          jsmith
          I think you missed the "Set Up the Project" step 6.

          6. Open FXMLTableView.java and edit the FXMLTableView class to look like Example 3-1.
          Example 3-1 FXMLTableView.java
          public class FXMLTableView extends Application {
              
              @Override
              public void start(Stage primaryStage) throws Exception {
                 primaryStage.setTitle("FXML TableView Example");
                 primaryStage.setScene
                 ((Scene)FXMLLoader.load(getClass().getResource("fxml_tableview.fxml")));
                 primaryStage.show();
              }
           
              public static void main(String[] args) {
                  launch(args);
              }
          }
          • 2. Re: Creating an Address Book with FXML tutorial error
            885691
            Your Java code assumes that the root of your FXML document is a Parent, but it is actually a Scene. Try replacing this:
            Parent root = FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));
            stage.setScene(new Scene(root));
            with this:
            Scene scene = FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));
            stage.setScene(scene);