在java.lang.reflect.Method.invoke(Method.java:497)"中获取此错误"在运行javafx程序时

时间:2016-09-06 18:22:16

标签: javafx

我是javafx的新手。我正在javafx中构建一个基本程序。 但是在java.lang.reflect.Method.invoke(Method.java:497)"中得到错误"在运行程序时........... 程序是:

package myjfxapp;

import com.sun.javaws.Main;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class NewFXMain extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {

FXMLLoader loader = new FXMLLoader(Main.class.getResource("main.fxml"));
        Pane root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

}

Fxml代码:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60">
   <children>
      <Button layoutX="221.0" layoutY="58.0" mnemonicParsing="false" text="Button" />
      <CheckBox layoutX="211.0" layoutY="152.0" mnemonicParsing="false" text="CheckBox" />
   </children>
</AnchorPane>

堆栈追踪:

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:497)
    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)

你的帮助将被迫......

1 个答案:

答案 0 :(得分:0)

您使用了

new FXMLLoader(Main.class.getResource(...));

由于某种原因导入com.sun.javaws.Main。您的班级名为NewFXMain,因此您需要

new FXMLLoader(NewFXMain.class.getResource(...));

或只是

new FXMLLoader(getClass().getResource(...));