tags:

views:

62

answers:

4

Okay, so this is the line that's returning null. What am I doing wrong while creating this FileInputStream?

FileInputStream fin = new FileInputStream(new File(getClass().getResource("data/levellocks.lv").toURI()));
+3  A: 

The only thing that can be null there is getResource("data/levellocks.lv") which is calling the toURI call to fail

willcodejavaforfood
but can it still be Null if the file is there? Cause I checked twice and it is O_O
William
it's not looking in the area you think it is
AHungerArtist
+2  A: 

Either getClass or getResource could return null. Everything else should succeed or throw an exception.

Roger Willcocks
+2  A: 

I actually just dealt with this issue (I'm no expert) but try debugging and see where the constructor is trying to resolve the name to. For me, it was the package of the class. So when I put the file in the expected folder, it found it.

Would probably be different for you, as I'm using maven. But I put it in src/main/resources and it couldn't find it. When I put a folder structure in src/main/resources of com.work.hin.terminology.match (which was the package of the class), it found it.

AHungerArtist
+2  A: 

Unless you really need a file input stream, you line can be simplified to:

InputStream in = getClass().getResourceAsStream("data/levellocks.lv");

Class.getResource() and Class.getResourceAsStream are relative to the package. To get the file relative to the root of the classpath, you can call those methods on the classloader:

InputStream in = getClass().getClassLoader().getResourceAsStream("data/levellocks.lv");

Did you make sure the file is in your binary folder, next to the .class files? Not just in your source folder next to the .java files?

nhnb