JavaFX“位置是必需的。” 即使在同一包装中
将文件移动到main / resources目录有效。
就我而言,以上所有都不是问题。
我更换时解决了我的问题:
getClass().getResource('ui_layout.fxml')
与:
getClass().getClassLoader().getResource('ui_layout.fxml')解决方法
我正在尝试运行JavaFX程序,但遇到了一些困难。我不断收到错误消息“ java.lang.NullPointerException:必须提供位置”。fxml文件与Application类位于同一软件包中。这是我非常简单的代码:
package com.kromalights.designer.entry;import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.stage.Stage;public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));primaryStage.setTitle("Kromalights Designer");primaryStage.setScene(new Scene(root,300,275));primaryStage.show(); } public static void main(String[] args) {launch(args); }}
这是我的main.fxml文件的副本:
<?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?><?import javafx.scene.layout.*?><?import javafx.scene.layout.BorderPane?><?scenebuilder-stylesheet mailStyles.css?><?import java.net.*?><BorderPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1"xmlns="http://javafx.com/javafx/2.2"fx:controller="com.kromalights.designer.entry.Controller"> <bottom><Pane prefHeight="200.0" prefWidth="200.0"/> </bottom> <center><Pane prefHeight="200.0" prefWidth="200.0"/> </center> <left><VBox prefHeight="200.0" prefWidth="100.0"/> </left> <top><HBox prefHeight="100.0" prefWidth="200.0"/> </top> <stylesheets><URL value="@mainStyles.css" /> </stylesheets></BorderPane>
控制器类确实存在,并且位于fxml文件中指定的包中。我所有的名字都是正确的,也是我认为应该的位置。我想念什么?我确实尝试重命名我的fxml文件,以防这是一个名称问题。请帮忙。仅供参考,我在OSX上使用Intellij IDEA。
更新:这是一个Maven问题。我为此项目安装了Maven,这引起了问题。我暂时删除了Maven,因此没有它我可以继续工作。有没有人对使用Maven时如何最好地解决这一问题有任何见解?
相关文章:
1. angular.js - angularjs 使用鼠标悬停时,标签一直闪2. angular.js - angularjs中如何实现单击一个span标签之后,拿到span标签中的内容?3. angular.js - angular内容过长展开收起效果4. html5 - 百度Ueditor代码高亮和代码段滚动条冲突是怎么回事?5. HTML 5输入框只能输入汉字、字母、数字、标点符号?正则如何写?6. docker绑定了nginx端口 外部访问不到7. javascript - 求解答,koa-bodyparser获取到的参数是空对象,为什么?????8. macos - mac下docker如何设置代理9. android - Genymotion 模拟器可以做屏幕适配检测吗?10. javascript - 关于Js中 this的一道题