为了账号安全,请及时绑定邮箱和手机立即绑定

JavaFX“位置是必需的。”即使它在同一个包中

JavaFX“位置是必需的。”即使它在同一个包中

潇湘沐 2019-08-05 14:04:52
JavaFX“位置是必需的。”即使它在同一个包中我试图让我的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时最好地处理这个问题?
查看完整描述

3 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

在我的情况下,以上所有都不是问题。

我替换时解决了我的问题:

getClass().getResource("ui_layout.fxml")

用:

getClass().getClassLoader().getResource("ui_layout.fxml")


查看完整回答
反对 回复 2019-08-05
?
撒科打诨

TA贡献1934条经验 获得超2个赞

将文件移动到main / resources目录工作。


查看完整回答
反对 回复 2019-08-05
?
绝地无双

TA贡献1946条经验 获得超4个赞

URL url = new File("src/main/java/ua/adeptius/goit/sample.fxml").toURL();Parent root = FXMLLoader.load(url);

这对我有帮助,因为

getClass.getResource("path")

总是让我无效;


查看完整回答
反对 回复 2019-08-05
  • 3 回答
  • 0 关注
  • 589 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号