JavaFX“位置是必需的。” 即使在同一包裝中
將文件移動(dòng)到main / resources目錄有效。
就我而言,以上所有都不是問(wèn)題。
我更換時(shí)解決了我的問(wèn)題:
getClass().getResource('ui_layout.fxml')
與:
getClass().getClassLoader().getResource('ui_layout.fxml')解決方法
我正在嘗試運(yùn)行JavaFX程序,但遇到了一些困難。我不斷收到錯(cuò)誤消息“ java.lang.NullPointerException:必須提供位置”。fxml文件與Application類(lèi)位于同一軟件包中。這是我非常簡(jiǎn)單的代碼:
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>
控制器類(lèi)確實(shí)存在,并且位于fxml文件中指定的包中。我所有的名字都是正確的,也是我認(rèn)為應(yīng)該的位置。我想念什么?我確實(shí)嘗試重命名我的fxml文件,以防這是一個(gè)名稱(chēng)問(wèn)題。請(qǐng)幫忙。僅供參考,我在OSX上使用Intellij IDEA。
更新:這是一個(gè)Maven問(wèn)題。我為此項(xiàng)目安裝了Maven,這引起了問(wèn)題。我暫時(shí)刪除了Maven,因此沒(méi)有它我可以繼續(xù)工作。有沒(méi)有人對(duì)使用Maven時(shí)如何最好地解決這一問(wèn)題有任何見(jiàn)解?
相關(guān)文章:
1. php - 微信開(kāi)發(fā)驗(yàn)證服務(wù)器有效性2. php如何獲取訪(fǎng)問(wèn)者路由器的mac地址3. 小程序怎么加外鏈,語(yǔ)句怎么寫(xiě)!求救新手,開(kāi)文檔沒(méi)發(fā)現(xiàn)4. javascript - 在 vue里面用import引入js文件,結(jié)果為undefined5. javascript - 我的站點(diǎn)貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?6. php - mysql 模糊搜索問(wèn)題7. python沒(méi)入門(mén),請(qǐng)教一個(gè)問(wèn)題8. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?9. html - 爬蟲(chóng)時(shí)出現(xiàn)“DNS lookup failed”,打開(kāi)網(wǎng)頁(yè)卻沒(méi)問(wèn)題,這是什么情況?10. javascript - js setTimeout在雙重for循環(huán)中如何使用?
