1

We are newcomers with JavaFX and Scene Builder and have the following problem.

If we define our controller in the FXML document (created with Scene Builder) we get the exception posted below.

Here you see the code for our Application, Controller and FXML document class.

Application class:

public class Projekt5 extends Application {

    public static Connection c= null;

    private static final String driverClass = "com.mysql.jdbc.Driver";
    private static final String connectionDescriptor = "jdbc:mysql://i-med-ts:3306/md_projekt";
    private static String user= "root";
    private static String password= "root";

    public void start(Stage stage) throws Exception {

        try{
            c = DriverManager.getConnection(connectionDescriptor, user,password);
            System.out.println("Connection");
        }

        catch (SQLException e) {
            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Information");
            alert.setHeaderText(null);
            alert.setContentText("A database error occured.");
            alert.showAndWait();
        }

        FXMLLoader loader= new FXMLLoader(getClass().getResource("/projekt5/FXMLDocument.fxml"));
        loader.setController(new FXMLDocumentController(c));
        AnchorPane root = loader.load();

        FXMLDocumentController control = new FXMLDocumentController(c);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

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

Controller class:

public class FXMLDocumentController implements Initializable {

    Connection c=null;
    private static PreparedStatement getUser = null;
    private static ResultSet rs = null;
    private TextField textName;
    private TextField textPassword;
    private Label nameLabel;
    private Label label;
    private Button button;

    public FXMLDocumentController (Connection con) {
        c = con;
    }

    public void loginApplication(ActionEvent event) {
        String Name = textName.getText();
        String Password = textPassword.getText();
        String queryCheck = "SELECT * "+ "FROM user_tab " + " WHERE user_id = ? "+ " and pw = ?";

        try {
            getUser  = c.prepareStatement(queryCheck);
            getUser.setString (1, Name);
            getUser.setString (2, Password);
            rs  = getUser.executeQuery();

            while (rs.next())
            {
                String name = rs.getString("user_id");
                String pw = rs.getString("pw");
            }
        } catch (SQLException e1) {
            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Information");
            alert.setHeaderText(null);
            alert.setContentText("A database error occured.");
            alert.showAndWait();
        }
    }

    public void handle(ActionEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");      //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

FXML (created with scene builder):

 <AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="900.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="projekt5.FXMLDocumentController">
    <children>
      <VBox layoutX="228.0" layoutY="100.0" prefHeight="208.0" prefWidth="425.0">
         <children>
            <GridPane>
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="24.0" minHeight="10.0" prefHeight="14.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="46.0" minHeight="10.0" prefHeight="46.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Label fx:id="nameLabel" prefHeight="30.0" prefWidth="61.0" text="Name" textOverrun="CLIP" GridPane.rowIndex="2">
                     <opaqueInsets>
                        <Insets />
                     </opaqueInsets>
                  </Label>
                    <Label fx:id="label" minHeight="16" minWidth="69" text="Password" GridPane.rowIndex="4" />
                  <TextField fx:id="textName" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <TextField fx:id="textPassword" GridPane.columnIndex="1" GridPane.rowIndex="4" />
               </children>
            </GridPane>
         </children>
      </VBox>
        <Button fx:id="button" contentDisplay="CENTER" layoutX="381.0" layoutY="352.0" onAction="#loginApplication" prefHeight="51.0" prefWidth="126.0" text="Log in " textAlignment="CENTER">
         <effect>
            <Blend />
         </effect>
         <contextMenu>
            <ContextMenu>
              <items>
                <MenuItem mnemonicParsing="false" text="Unspecified Action" />
              </items>
            </ContextMenu>
         </contextMenu>
      </Button>
    </children>
   <effect>
      <Blend />
   </effect>
</AnchorPane>

Throws this exception:

 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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: Controller value already specified.
file:/home/aadalen/NetBeansProjects/projekt5/dist/run550935368/projekt5.jar!/projekt5/FXMLDocument.fxml:16

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:914)
    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)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at projekt5.Projekt5.start(Projekt5.java:49)
    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.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    ... 1 more
Exception running application projekt5.Projekt5
Java Result: 1
anothernode
  • 5,100
  • 13
  • 43
  • 62
jaskrimi
  • 17
  • 4

0 Answers0