首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决javaFx中的TARGETINVOCATIONEXCEPTION问题?

如何解决javaFx中的TARGETINVOCATIONEXCEPTION问题?
EN

Stack Overflow用户
提问于 2017-12-23 07:56:17
回答 2查看 279关注 0票数 1

在运行程序时,我的JavaFX中出现了一个与FXML代码混淆的错误。

我有三个文件:

第一个名为:F2XML.fxml,包含以下代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafxapplication5.FXMLDocumentController ?>
<GridPane fx:controller = "FXMLDocumentController" hgap="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="600.0" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <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 maxHeight="20.0" minHeight="8.0" prefHeight="8.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="12.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
    <children>
        <Label text="Choose gender:" />
        <RadioButton mnemonicParsing="false" text="Female" GridPane.rowIndex="2">
            <toggleGroup>
                <ToggleGroup fx:id="gender" />
            </toggleGroup>
        </RadioButton>
        <RadioButton mnemonicParsing="false" selected="true" text="Male" toggleGroup="$gender" GridPane.rowIndex="1" />
        <Button mnemonicParsing="false" onAction="" onMouseClicked="#BClickedActionHandler" text="OK" GridPane.rowIndex="4" />
    </children>
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>
</GridPane>

第二个文件名为:FXMLDocumentController.java,它包含:

代码语言:javascript
复制
package javafxapplication5;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;

public class FXMLDocumentController implements Initializable {
    @FXML
    RadioButton Male;
    @FXML
    RadioButton Female;
    @FXML
    Button OK;

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

    @FXML
    public void BClickedActionHandler() {
        System.out.println("Hello");
    }

}

第三个是:JavaFXApplication5.java,它包含:

代码语言:javascript
复制
package javafxapplication5;

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

public class JavaFXApplication5 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = null;
        Scene scene = null;
        FXMLLoader loader = new FXMLLoader(getClass().getResource("F2XML.fxml"));
        loader.setController(new FXMLDocumentController());
        Pane mainPane = loader.load();
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }

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

}

在运行该程序时,我在处理以下代码:

代码语言:javascript
复制
Executing D:\Java\JavaFXApplication5\dist\run1638782428\JavaFXApplication5.jar using platform C:\Program Files\Java\jdk1.8.0_151\jre/bin/java
Exception in Application start method
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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
file:/D:/Java/JavaFXApplication5/dist/run1638782428/JavaFXApplication5.jar!/javafxapplication5/F2XML.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at javafxapplication5.JavaFXApplication5.start(JavaFXApplication5.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,1]
Message: Premature end of file.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
    at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
    ... 12 more
Exception running application javafxapplication5.JavaFXApplication5
Java Result: 1

有人能帮忙解决这个错误吗?

EN

回答 2

Stack Overflow用户

发布于 2017-12-23 08:14:40

在堆栈跟踪中写着:

代码语言:javascript
复制
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,1]
Message: Premature end of file.

这意味着您的输入文件不像解析器所期望的那样。将其加载到XML编辑器,甚至Internet Explorer中,以查看您的文件是否有效。

票数 0
EN

Stack Overflow用户

发布于 2017-12-23 09:20:18

在编译时执行未知代码的方法中抛出InvocationTargetExceptions,因此方法引发的异常不能在抛出子句中声明或在调用时捕获。因此,您必须“向下”堆栈跟踪,以查找相关的“由”-block引起的。

在这种情况下

代码语言:javascript
复制
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,1]
Message: Premature end of file.

解析的XML似乎已损坏。我检查了您在问题中提供的XML,这在我看来还可以(如果出现这样的错误,也可以向投诉)。

请确保您在这里提供的XML实际上是驻留在Jar文件中的XML,正如您在“上面”异常块中看到的那样:

代码语言:javascript
复制
Caused by: javafx.fxml.LoadException: 
file:/D:/Java/JavaFXApplication5/dist/run1638782428/JavaFXApplication5.jar!/javafxapplication5/F2XML.fxml

也许您的XML在文件系统中,但是对应的类路径条目在JavaFXApplication5.jar之后,并且您将错误的XML视为错误的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47950822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档