javafx运行时失败

时间:2019-03-12 09:57:00

标签: java javafx

大家好,我一直在NetBeans 8.2和Gluon Scene Builder上开发JavaFx应用程序。最初,程序给我输出。开发后,我收到了错误消息。

(我在这个平台上尝试了很多建议,清理并构建了项目,更改了fxml文件的位置,删除并重新上传了Scene Builder,甚至删除了整个项目,然后再次写下来看看我是否m忘记了什么。该程序没有任何语法错误。我的服务器和客户端都在运行。运行JavaFXMLApplication.java时发生错误)

我可能缺少一些东西,没错。

HP的用户变量

PATH:   C:\Users\HP\Envs\gui; C:\Users\HP\Desktop\jdk-11.0.2

系统变量

PATH:C:\Program Files\Python37\Scripts\;C:\Program 
Files\Python37\;C:\Users\HP\Envs\gui;C:\Program 
Files\Java\jdk1.8.0_111\bin;C:\Program Files\Java\jdk-11.0.2\bin

我的fxml文件

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="javafxmlapplication.FXMLDocumentController">

代码:

package javafxmlapplication;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class JavaFXMLApplication extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource
    ("/src/javafxmlapplication/fxml/FXMLDocument.fxml"));

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

        stage.setTitle("GUI");
        stage.setScene(scene);
        stage.show();

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask(){
            @Override
            public void run(){
                Random rand = new Random();
                int myrand = rand.nextInt(15)+1;
                System.out.println((myrand+1)+"blue");
                root.getChildrenUnmodifiable().get(myrand).setStyle("-fx-background-color: #001f3f");
                    try{
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e){
                        //e.printStackTrace();
                        System.out.println(e);
                    }
                root.getChildrenUnmodifiable().get(myrand).setStyle("-fx-background-color : #FF4136");
            }
        },0,1000);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

在错误消息系统中标记了这一行,但行号没有错误(我尝试添加getClassLoader(),它给了我同样的错误)

    Parent root = FXMLLoader.load(getClass().getResource
    ("/src/javafxmlapplication/fxml/FXMLDocument.fxml"));

我的系统在cmd中带有java和javac代码,输出为正。

Executing C:\Users\HP\Documents\NetBeansProjects\JavaFXMLApplication\dist\run1672502609\JavaFXMLApplication.jar using platform C:\Program Files\Java\jdk1.8.0_111\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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    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 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.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application javafxmlapplication.JavaFXMLApplication
Java Result: 1
Deleting directory C:\Users\HP\Documents\NetBeansProjects\JavaFXMLApplication\dist\run1672502609
jfxsa-run:
BUILD SUCCESSFUL (total time: 2 seconds)

以下错误

javafxmlapplication.JavaFXMLApplication.start(JavaFXMLApplication.java:25)

指出这一点

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("/src/javafxmlapplication/fxml/FXMLDocument.fxml"));

Project structure

Scene builder connection

任何人的帮助我都会陷入困境。

1 个答案:

答案 0 :(得分:0)

您使用错误的路径来加载fxml文件。使用fxml/FXMLDocument.fxml/javafxapplication/fxml/FXMLDocument.fxml

  

查找具有给定名称的资源。搜索与给定类关联的资源的规则由该类的定义类加载器实现。该方法委托给该对象的类加载器。如果此对象是由引导类加载器加载的,则该方法将委托给ClassLoader.getSystemResource(java.lang.String)。   在委派之前,使用以下算法从给定资源名称构造绝对资源名称:

     
      
  • 如果名称以'/'开头('\ u002f'),则资源的绝对名称是名称中'/'之后的部分。
  •   
  • 否则,绝对名称的格式如下:modified_pa​​ckage_name / name
  •   

来自javadoc

因此,以下方法应该起作用:

Parent root = FXMLLoader.load(getClass().getResource("fxml/FXMLDocument.fxml"));

或者:

Parent root = FXMLLoader.load(getClass().getResource("/javafxapplication/fxml/FXMLDocument.fxml"));