大家好,我一直在 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"));
任何帮助将不胜感激,我正陷入困境。
最佳答案
您使用了错误的路径来加载 fxml 文件。使用fxml/FXMLDocument.fxml
或/javafxapplication/fxml/FXMLDocument.fxml
。
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResource(java.lang.String). Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
- If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
- Otherwise, the absolute name is of the following form: modified_package_name/name
来自javadoc .
所以以下应该有效:
Parent root = FXMLLoader.load(getClass().getResource("fxml/FXMLDocument.fxml"));
或者:
Parent root = FXMLLoader.load(getClass().getResource("/javafxapplication/fxml/FXMLDocument.fxml"));
关于javafx 运行时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55118628/