【发布时间】:2019-08-02 18:10:11
【问题描述】:
大家好,我一直在 NetBeans 8.2 和 Gluon Scene Builder 上开发 JavaFx 应用程序。最初程序给了我一个输出。开发后我收到错误。
(我在这个平台上尝试了很多建议,我清理并构建了项目,我更改了我的 fxml 文件位置,我删除并重新上传了 Scene Builder,我什至删除了整个项目并再次写下来看看我是否'我忘记了一些东西。该程序不包含任何语法错误。我的服务器和客户端都在工作。运行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"));
任何帮助将不胜感激我在循环中。
【问题讨论】:
-
a) 不要从后台线程修改 GUI。如果您想实现从
TimerTask更新gui,请使用Platform.runLater(请务必从runnable 中排除像Thread.sleep这样的长时间运行的操作;您可能需要使用Platform.runLater两次)。Parent root = FXMLLoader.load(getClass().getResource("/javafxmlapplication/fxml/FXMLDocument.fxml"));应该可以工作,除非 Netbeans 不包含该资源或者我错过了一个错字;如果没有,请使用 Netbeans 的文件浏览器查看生成的 jar 并检查资源 (\dist\run***\JavaFXMLApplication.jar) -
没有
run***文件人只有:web-files 文件、.html、jar.jnpl -
它在您发布的错误上方显示
Executing C:\Users\HP\Documents\NetBeansProjects\JavaFXMLApplication\dist\run1672502609\JavaFXMLApplication.jar using ...。罐子应该在附近的某个地方...... -
是的,有一个 jar 文件,但我不知道这个文件是什么。你能澄清一下吗?
-
在我使用的 Netbeans 版本中,您可以打开
.jar文件并使用Projects旁边的Files选项卡查看其内容。您可以使用此功能来验证 fxml 是否在您想要的位置或找出它的放置位置。 (请记住,您在那里看到的.s 对应于资源路径中的/s,您需要在路径前面添加/以使getResource在根目录中开始查找。) .通过这种方式,您可以验证这不仅仅是项目设置不包含 fxml 文件作为资源的问题。 (不过先检查不同的路径。)