This site requires JavaScript, please enable it in your browser!
Greenfoot back
alkwmd21ij
alkwmd21ij wrote ...

2020/4/28

Loading file from classpath results in NullPointerException

alkwmd21ij alkwmd21ij

2020/4/28

#
I programmed something in Eclipse and have to move it over to Greenfoot. I simply copied the code into new classes within Greenfoot while always making sure the package statement is not copied and added the necessary libraries. There were no errors this far but when I try to run the program, it doesn't find the file / can't read from it. I read from the file like this:
try(BufferedReader rd = new BufferedReader(new InputStreamReader(FileUtils.class.getResourceAsStream(path))))
where path is "/shaders/mainVertex.txt" Why can the file not be read from? I've tried many different paths (e.g. removing "/" in front or moving file to the project's directory and changing the path to just the file name "mainVertex.txt"), but couldn't get a single one to work. Is it not able to find the file because it's not within the package? Which is the package and how could I put the files inside of the package? In the Eclipse project, I had this package / resources configuration. FileUtils is where the problem occurs:
danpost danpost

2020/4/28

#
Try replacing "FileUtils.class" with "getClass().getClassLoader()".
You need to login to post a reply.