简体   繁体   English

应用程序启动方法 JavaFX 1.8 中的异常

[英]Exception in Application start method JavaFX 1.8

I'm having trouble running JavaFX Program which runs on Java version 1.8.我在运行在 Java 1.8 版上运行的 JavaFX 程序时遇到问题。 I noticed that even if I can run this program on Intellij IDE, it throws我注意到即使我可以在 Intellij IDE 上运行这个程序,它也会抛出

java.lang.reflect.InvocationTargetException

error when I try running it in command line.当我尝试在命令行中运行它时出错。 There are no errors when I use "javac Main.java" but it throws the error after I compile it then type "java Main.java"当我使用“javac Main.java”时没有错误,但是在我编译它然后输入“java Main.java”后它会抛出错误

Here are the files这是文件

src folder源文件夹

  • controller folder控制器文件夹

    • Controller.java控制器.java
  • model folder模型文件夹

    • Database.java数据库.java
  • view folder查看文件夹

    • loginview.fxml登录视图文件

    • registerview.fxml注册视图文件

    • mainmenu.fxml主菜单文件

  • Main.java主程序

Main.java主程序

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

import model.Database;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("view/mainmenu.fxml"));
        primaryStage.setTitle("Test Program");
        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        Database db = new Database();
        db.testmethod();

        launch(args);
    }
}

Controller.java控制器.java

package controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Controller {

    public void exitProgram() {
        System.exit(0);
    }

    public void openRegister(ActionEvent event) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getClassLoader().getResource("view/registerview.fxml"));
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Register User");
            stage.setScene(new Scene(root, 600, 600));
            stage.setResizable(false);
            stage.show();

            close(event);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void openLogin(ActionEvent event) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getClassLoader().getResource("view/loginview.fxml"));
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Login User");
            stage.setScene(new Scene(root, 600, 600));
            stage.setResizable(false);
            stage.show();

            close(event);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void close(ActionEvent event) {
        ((Node) (event.getSource())).getScene().getWindow().hide();
    }
}

Database.java数据库.java

package model;

public class Database {

    public void testmethod(){
        System.out.println("TEST METHOD");
    }

}

loginview.fxml登录视图文件

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
   <children>
      <Label layoutX="134.0" layoutY="145.0" prefHeight="165.0" prefWidth="332.0" text="LOGIN" />
   </children>
</Pane>

mainmenu.fxml主菜单文件

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
   <children>
      <Button fx:id="buttonReg" layoutX="32.0" layoutY="60.0" mnemonicParsing="false" onAction="#openRegister" prefHeight="104.0" prefWidth="521.0" text="OPEN REGISTER" />
      <Button fx:id="buttonLogin" layoutX="32.0" layoutY="211.0" mnemonicParsing="false" onAction="#openLogin" prefHeight="104.0" prefWidth="521.0" text="OPEN LOGIN" />
      <Button fx:id="buttonExit" layoutX="32.0" layoutY="356.0" mnemonicParsing="false" onAction="#exitProgram" prefHeight="104.0" prefWidth="521.0" text="EXIT" />
   </children>
</Pane>

registerview.fxml注册视图文件

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

   <?import javafx.scene.control.Label?>
   <?import javafx.scene.layout.Pane?>


   <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
      <children>
         <Label layoutX="130.0" layoutY="145.0" text="REGISTER" />
      </children>
   </Pane>

The error in command line命令行中的错误

TEST METHOD
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$1(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/C:/Users/Bryan/Desktop/TESTRUN/src/view/mainmenu.fxml:6

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
        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.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at Main.start(Main.java:13)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$3(WinApplication.java:177)
        ... 1 more
Caused by: java.lang.ClassNotFoundException: controller.Controller
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
        ... 22 more
Exception running application Main

I noticed that Database runs before throwing the Error.我注意到数据库在抛出错误之前运行。 I don't understand what's the cause of this error我不明白这个错误的原因是什么

on Main.java,在 Main.java 上,

add添加

import controller.Controller

Instead of having buttons like "Buttons" try to put them with the class "JFXButton"尝试将它们与类“JFXButton”放在一起,而不是像“Buttons”这样的按钮

You have this:你有这个:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
    <Button fx:id="buttonReg" layoutX="32.0" layoutY="60.0" mnemonicParsing="false" onAction="#openRegister" prefHeight="104.0" prefWidth="521.0" text="OPEN REGISTER" />
    <Button fx:id="buttonLogin" layoutX="32.0" layoutY="211.0" mnemonicParsing="false" onAction="#openLogin" prefHeight="104.0" prefWidth="521.0" text="OPEN LOGIN" />
    <Button fx:id="buttonExit" layoutX="32.0" layoutY="356.0" mnemonicParsing="false" onAction="#exitProgram" prefHeight="104.0" prefWidth="521.0" text="EXIT" />
</Pane>

Try this:尝试这个:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
    <JFXButton fx:id="buttonReg" layoutX="32.0" layoutY="60.0" mnemonicParsing="false" onAction="#openRegister" prefHeight="104.0" prefWidth="521.0" text="OPEN REGISTER" />
    <JFXButton fx:id="buttonLogin" layoutX="32.0" layoutY="211.0" mnemonicParsing="false" onAction="#openLogin" prefHeight="104.0" prefWidth="521.0" text="OPEN LOGIN" />
    <JFXButton fx:id="buttonExit" layoutX="32.0" layoutY="356.0" mnemonicParsing="false" onAction="#exitProgram" prefHeight="104.0" prefWidth="521.0" text="EXIT" />
</Pane>

The InvocationTargetException is occurring because the call to method getResource() – in method openRegister() of class Controller – is returning null. InvocationTargetException的发生是因为对方法getResource()的调用——在类Controller openRegister()方法中——返回 null。 You can check this by simply printing out the value returned by method getResource() , for example您可以通过简单地打印出方法getResource()返回的值来检查这一点,例如

System.out.println(getClass().getClassLoader().getResource("view/registerview.fxml"));

It is returning null because method getResource() builds an absolute path to the file registerview.fxml and then checks to see whether such a file exists.它返回 null 因为方法getResource()构建文件registerview.fxml的绝对路径,然后检查是否存在这样的文件。 The absolute path that method getResources() builds is not the actual path to the file.方法getResources()构建的绝对路径不是文件的实际路径。 Hence you need to change the method argument, ie "view/registerview.fxml"因此您需要更改方法参数,即“view/registerview.fxml”

I'm guessing that the below solution is not the only solution, but it worked for me.我猜下面的解决方案不是唯一的解决方案,但它对我有用。

In order to prevent getting the InvocationTargetException I did the following.为了防止获得InvocationTargetException我执行了以下操作。

  1. I put class Main in a package which I named jfxtests .我将Main类放在一个名为jfxtests的包中。
    (It is not clear to me, from your question, whether Main is in a package or not.) (根据您的问题,我不清楚Main是否在包中。)
  2. I made the controller folder and the view folder sub-packages of jfxtests .我制作了jfxtestscontroller文件夹和view文件jfxtests包。
  3. I call method getClass().getResource() and not getClass().getClassLoader().getResource() .我调用方法getClass().getResource()而不是getClass().getClassLoader().getResource()
  4. In class Controller , I changed the argument that I pass to method getResource() .在类Controller ,我更改了传递给方法getResource()

The only change I made was to class Controller .我所做的唯一更改是对Controller类进行更改。 Here it is with my changes:这是我的更改:

package jfxtests.controller;  // changed package

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Controller {

    public void exitProgram() {
        System.exit(0);
    }

    public void openRegister(ActionEvent event) {
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("/jfxtests/view/registerview.fxml"));  // changed this line
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Register User");
            stage.setScene(new Scene(root, 600, 600));
            stage.setResizable(false);
            stage.show();

            close(event);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void openLogin(ActionEvent event) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getResource("/jfxtests/view/loginview.fxml"));  // changed this line
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Login User");
            stage.setScene(new Scene(root, 600, 600));
            stage.setResizable(false);
            stage.show();

            close(event);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void close(ActionEvent event) {
        ((Node) (event.getSource())).getScene().getWindow().hide();
    }
}

Note that since I changed the package for class Controller , I also had to change the FXML files.请注意,由于我更改了类Controller的包,因此我还必须更改 FXML 文件。

fx:controller="jfxtests.controller.Controller"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM