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 - 如何通俗易懂的解釋“依賴注入”?2. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?3. 老哥們求助啊4. 我的html頁面一提交,網頁便顯示出了我的php代碼,求問是什么原因?5. django - 后臺返回的json數據經過Base64加密,獲取時用python如何解密~!6. html5 - angularjs中外部模版加載無法使用7. css3 - 請問一下在移動端CSS布局布局中通常需要用到哪些元素,屬性?8. tp6表單令牌9. node.js - node 客戶端socket一直報錯Error: read ECONNRESET,用php的socket沒問題哈。。10. ubuntu PPA 更新老是不成功該怎么辦
