IntelliJ IDEA - getClass().getResource(“…”) return null


IntelliJ IDEA - getClass().getResource(“…”) return 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.



This is the hierarchy :



dz.bilaldjago.homekode.MainApp.java



dz.bilaldjago.homekode.view.RootLayout.FXML



This is the code snippet I used:


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



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



the result is the same. Any idea please





possible duplicate of Java - class.getResource returns null
– maja
Apr 11 '15 at 18:23




5 Answers
5



For those who use Intellij Idea: check for Settings -> Compiler -> Resource patterns.


Settings -> Compiler -> Resource patterns



The setting contains all extensions that should be interpreted as resources. If an extension does not comply to any pattern here, class.getResource will return null for resources using this extension.





I add fxml files using this pattern ?*.fxml but the result is the same!
– BilalDja
Oct 12 '14 at 18:13





Why "?*.fxml" and not "*.fxml" or "view:*" - all files and folders within the directory view. see : jetbrains.com/idea/webhelp/compiler.html#pattern
– stacky
Oct 12 '14 at 18:21






I follow the same pattern exist in the Resources field
– BilalDja
Oct 12 '14 at 20:27





it doesn't make any change!
– BilalDja
Oct 12 '14 at 20:31





@BilalDja did you manage to fix this problem? As i am experiencing the same issue right now
– Metin Dagcilar
Dec 20 '17 at 0:09



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.


Right click


Mark directory As


Resource Root



Recompile & rejoice :P Hope this working for you~





simple but worked for miiii :D
– truongnm
Mar 4 '17 at 7:47





did not work for me, i changed the Compiler resource patterns. Made the resource folder both as the root folder of my project and then the working folder where my Class is and still didn't work! please help. I am using intellij
– Metin Dagcilar
Dec 20 '17 at 0:11






@AVERAGE Hi, I don't think that would be a problem. Have you tried re-creating a project, and redo your operation? If it still doesn't work, please post your detailed operation steps. That would be helpful for solving your issue.
– MewX
Dec 28 '17 at 10:25



I gave up trying to use
getClass().getResource("BookingForm.css"));


getClass().getResource("BookingForm.css"));



Instead I create a File object, create a URL from that object and then pass it into getStyleSheets() or setLocation()
File file = new File(System.getProperty("user.dir").toString() + "/src/main/resources/BookingForm.css");



File file = new File(System.getProperty("user.dir").toString() + "/src/main/resources/BookingForm.css");



scene.getStylesheets().add(folder.toURI().toURL().toExternalForm());


scene.getStylesheets().add(folder.toURI().toURL().toExternalForm());



if your project is a maven project, check the target code to see whether your .fxml file exist there. if it's not there ,just add


<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>



in your pom.xml



Windows is case-sensitive, the rest of the world not. Also an executable java jar (zip format) the resource names are case sensitive.



Best rename the file


view/RootLayout.FXML



to


view/RootLayout.fxml



This must be done by moving the original file away, and creating a new one.



Also compile to a jar, and check that the fxml file was added to the jar (zip file). When not IntelliJ resource paths are treated by an other answer.



By the way this is path relative to the package path of getClass(). Be aware if you extended this class the full path changes, better use:


getClass()


MainApp.class.getResource("view/RootLayout.fxml")






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift