IntelliJ IDEA - getClass().getResource("...") 返回 null [英] IntelliJ IDEA - getClass().getResource("...") return null

查看:52
本文介绍了IntelliJ IDEA - getClass().getResource("...") 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 IntelliJ IDEA 13.1.5,我曾经使用过 Eclipse.我正在开发 JavaFX 应用程序,我尝试使用 getClass().getResource() 在我的 MainApp 类中加载 FXML 文件.我阅读了文档并尝试了几个想法,最后我 null.

I'm using IntelliJ IDEA 13.1.5, I used to work with Eclipse. I'm working on JavaFX application, I try to load FXML file within my MainApp class using getClass().getResource(). I read the documentation and I try several idea, at the end I have null.

这是层次结构:

dz.bilaldjago.homekode.MainApp.java

dz.bilaldjago.homekode.MainApp.java

dz.bilaldjago.homekode.view.RootLayout.FXML

dz.bilaldjago.homekode.view.RootLayout.FXML

这是我使用的代码片段:

This is the code snippet I used:

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/RootLayout.fxml"));

我尝试了其他解决方案,例如从根目录提供 url 并使用 classLoader

I tried other solution such giving the url from the root and using the classLoader

结果是一样的.任何想法请

the result is the same. Any idea please

推荐答案

我通过指出IDEA上的资源根解决了这个问题.

I solved this problem by pointing out the resource root on IDEA.

右键单击目录(或只是项目名称)-> 将目录标记为 -> Resource Root.

Right click on a directory (or just the project name) -> Mark directory As -> Resource Root.

重新编译 &高兴 :P 希望这对你有用~

Recompile & rejoice :P Hope this working for you~

这篇关于IntelliJ IDEA - getClass().getResource("...") 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆