5

I've made a function(Java) that is supposed to read bytes from a file and print them to the console:

public void loadPixels(int size){
    ClassLoader cl = this.getClass().getClassLoader();
    pixels = new byte[size];
    try{
        InputStream stream = cl.getResource("res/" + fileName).openStream();
        stream.read(pixels);
        System.out.println(pixels.toString());
        stream.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

The problem is, I'm getting a NullPointerException on the line

InputStream stream = cl.getResource("res/" + fileName).openStream();

For the file I'm trying to open, the name is "font.spt", which is also the value held by fileName. This file is within the folder "res" in the project's root directory, and I'm currently using the Eclipse IDE.

Is my approach to the path for the file wrong, or is something else the issue?

To recap: fileName points to "font.spt", which is under the "res" folder in the bin directory.

EDIT: the "res" folder containing the .spt file is now under the "bin" for the project, rather than the root directory, but I still get the error. When running from the IDE or as an exported .jar, I still get the NullPointerException, where am I supposed to put these files? Can someone give me a screenshot or example?

4
  • You can open the .jar file with a zipping tool and find the path where your file is or do you have a web project? Commented Feb 2, 2015 at 17:59
  • 2
    The ClassLoader loads resources from the classpath. If the file is at the root of the project, it's not compiled by Eclipse to the output directory, and is thus not in the classpath at runtime. Put the file in the sources directory if you want it to be in the classpath at runtime. Commented Feb 2, 2015 at 18:05
  • with "res/" + filename, does that mean I'd want to make a folder under "src" named "res", then put the files in there? Commented Feb 2, 2015 at 18:11
  • Yes. Put it within your src folder Commented Feb 2, 2015 at 18:21

1 Answer 1

7

By default, Eclipse will copy all non .java files it finds in the project's Source Locations to the Output Folder when it builds. So one option is to just place your resource files under the source folder along with your source code. However, a better option is to use a separate resources folder for non-Java files and declare that as an additional Source Location (via your project's Java Build Path properties). That's how Maven and Gradle projects are organized, too.

For example, you might have:

MyProject\
    src\
        java\
            com\
                ....*.java
        resources\
            fonts\
                font.spt

With both src\java\ and src\resources\ defined as Source Locations in the Build Path. Then your code to load the file would be like:

getResource("fonts/" + fileName)

Something to consider: use Gradle or Maven to manage your builds, both of which enforce/provide a project structure similar to this anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.