引起:javafx.fxml.LoadException错误

时间:2016-01-07 16:39:15

标签: java

此代码有效,但现在我遇到了麻烦。 我认为这是问题,但是idk:

Caused by: javafx.fxml.LoadException: 
    /C:/Users/alex/workspace/FXMLExample/bin/application/login.fxml:10

我不知道/明白这些代码有什么问题。在主要只是运行场景。 »Main.java

package application;

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

public class Main extends Application {

    Parent root;

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

        Scene scene = new Scene(root, 800, 600);

        stage.setTitle("Login system");
        stage.setScene(scene);
        stage.show();
    }

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

»login.fxml,我认为这是问题

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxmlexample.Buton"
    alignment="center" hgap="10" vgap="10" styleClass="root">
    <padding>
        <Insets top="25" right="25" bottom="10" left="25" />
    </padding>

    <Text fx:id="bunvenit" text="Bun venit!" GridPane.columnIndex="0"
        GridPane.rowIndex="0" />

    <Text fx:id="text" text="Logheaza-te folosind datele tale personale:"
        GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnSpan="2" />

    <Label text="Username:" GridPane.columnIndex="0"
        GridPane.rowIndex="2" />

    <TextField fx:id="username" GridPane.columnIndex="1"
        GridPane.rowIndex="2" />

    <Label text="Parola:" GridPane.columnIndex="0" GridPane.rowIndex="3" />

    <PasswordField fx:id="parola" GridPane.columnIndex="1"
        GridPane.rowIndex="3" />

    <HBox spacing="10" alignment="bottom_right" GridPane.columnIndex="1"
        GridPane.rowIndex="5">
        <Button text="Logheaza-te" onAction="#loginButton" />
    </HBox>

    <Text fx:id="eroare" GridPane.columnIndex="1" GridPane.rowIndex="7" />

    <Button text="Am uitat parola" GridPane.columnIndex="0"
        GridPane.rowIndex="8" onAction="#passwordButton" />

    <Button text="Creeaza un cont nou" GridPane.columnIndex="1"
        GridPane.rowIndex="8" onAction="#createAccountButton" />

    <stylesheets>
        <URL value="@application.css" />
    </stylesheets>

</GridPane>

»Buton.java,所有按钮动作

package fxmlexample;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public abstract class Buton {

    private static final int SCENE_HEIGHT = 600;
    private static final int SCENE_WIDTH = 800;
    @FXML
    private Text eroare;
    @FXML
    private TextField username;
    @FXML
    private PasswordField parola;
    @FXML
    private Parent root;

    /* loginButton */
    @FXML
    protected void loginButton(ActionEvent event) throws IOException {
        boolean gasit = false;
        try (BufferedReader buf = new BufferedReader(new    FileReader("data//in//users.txt"))) {
            String linie;
            gasit = false;
            while ((linie = buf.readLine()) != null) {
                String part[] = linie.split(" ");
                if (username.getText().equals(part[0]) && parola.getText().equals(part[1])) {
                    gasit = true;
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (gasit) {
            Parent root = FXMLLoader.load(getClass().getResource("../application/logged.fxml"));
            Scene scene = new Scene(root, SCENE_WIDTH, SCENE_HEIGHT);
            Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
            stage.setScene(scene);
            stage.setTitle("Felicitari, te-ai logat cu succes!");
            stage.show();
        } else {
            eroare.setText("Username sau parola incorecta!");
        }
    }
}

»错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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.lambda$launchApplication$156(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/C:/Users/alex/workspace/FXMLExample/bin/application/login.fxml:10

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.access$700(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:15)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
    ... 1 more
Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
    ... 23 more
Exception running application application.Main

1 个答案:

答案 0 :(得分:1)

它清楚地说:

java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)

它无法实例化您的抽象Buton类。使其具体化。