AnswerBun.com

Scala getClass.getResource() returning null

I have this code:

val url: URL = getClass.getResource("com/mysite/main/test.fxml")

and it always returns null (or Unit). I have only two files in the project:

MyProj/src/com/mysite/main/Test.scala
MyProj/src/com/mysite/main/test.fxml

and when I run the Test.scala the url value is always null.

I just tried rebuild the project, I am using IntelliJ IDEA. What am I doing wrong here?

Stack Overflow Asked by Tower on January 3, 2021

4 Answers

4 Answers

You have three options:

  • take advantage of relative path to your current package (where Test.class is):

    getClass.getResource("test.fxml")
    
  • you can use absolute path:

    getClass.getResource("/com/mysite/main/test.fxml")
    
  • or load through the ClassLoader (note that it always start from root):

    getClass.getClassLoader.getResource("com/mysite/main/test.fxml")
    

In IntelliJ IDEA, make sure you have added ;?*.fxml to the:

Settings (Preferences on Mac) | Compiler | Resource Patterns.

Correct answer by Tomasz Nurkiewicz on January 3, 2021

If it does not work, you can try with ClassLoader:

ClassLoader.getSystemResource("filename").getPath

The filename should be in the same directory layer

Answered by Newt on January 3, 2021

Late answer but I just had this same problem. The root cause was an incorrect rootProject.name entry in my settings.gradle. Once I fixed that, cleaned, and rebuilt my resource were able to load using getClass().getResource(). Hopefully that helps somebody.

Answered by cory.todd on January 3, 2021

Possibly it's not being copied to the bin/ directory from the src/ directory? This happens on recompilation, but if you drop it into the src/ directory after the program is already compiled, the IDE won't know.

Answered by ConorR on January 3, 2021

Add your own answers!

Related Questions

Python, change a var by a string

4  Asked on February 21, 2021 by beardlongo

   

Vue – how to show an array of data in a table?

2  Asked on February 21, 2021 by jayk23

     

CMake unit testing – Unresolved external symbol

1  Asked on February 21, 2021 by chrispytoes

   

Break string into new rows after grepl

2  Asked on February 21, 2021 by ip2018

   

Send unicode sms with java

3  Asked on February 21, 2021 by hong4rc

     

Getting n1 n2 outputs after conversion

2  Asked on February 21, 2021 by icemilo

   

Is that possible to make for each user table in database

0  Asked on February 21, 2021 by haddadi-abdraouf

     

Run-time Error 1004; Error handling

2  Asked on February 20, 2021 by karl-drews

       

Password Generating program in python

2  Asked on February 20, 2021 by m-ismail

         

Error TS1086:An accessor cannot be declared in an ambient context

0  Asked on February 20, 2021 by leonardokunkel

 

Pass object data as FormData

2  Asked on February 20, 2021 by swarup-chavan

   

Iteration in Python throws results no desired

1  Asked on February 20, 2021 by catalina-hernndez

         

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.