0

I am very new to StackOverflow and I've done my best to fix this problem before posting this question here. I'm faced with the problem of getResource() returning null. I have a hunch that this is because I'm on a mac and the pathing is different here than on a PC (where this code seems to work fine). This is the code:

public class SampleClass
{
    static String imgpath = "/theimage.png";

    public static void main(String[] args)
    {
        System.out.println(imgpath);
        System.out.println(SampleClass.class.getResource(imgpath));
        try
        {
            BufferedImage image = ImageIO.read(SampleClass.class.getResource(imgpath));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

}

src, res and bin are all in the same directory and theimage.png is inside of res.

System.out.println(SampleClass.class.getResource("imgpath")); gives me null.

Xuid
  • 21
  • 5
  • 7
    Are you aware that there is a massive difference between `getResource("imgpath")` and `getResource(imgpath)` ? – Boann Sep 30 '14 at 21:46
  • Yes, sorry, I must have made a mistake when changing the variable names. In the original code there are no "". When removing them the problem still persists. – Xuid Oct 01 '14 at 05:39
  • With the path `"/theimage.png"` you are using, the file need to be directly in the src, i.e. `src/theimage.png`. The `/` brings the search to the root of the classpath, which is (before build), the src – Paul Samsotha Oct 01 '14 at 05:43
  • I moved theimage.png from res to src. Still having the same error. Thank you for the response though. – Xuid Oct 01 '14 at 05:53
  • It _should_ work. It is the correct way. You can still have it in the res if you want, but make sure the res is in the src `src/res/theimage.png` and use `getResource("/res/theimage.png");`. This, given everything else is correct, always works. So check everyhing else – Paul Samsotha Oct 01 '14 at 05:59
  • That's very odd, perhaps I have misunderstood you? src, bin and res are now in the same folder and I have moved theimage.png from res to src. – Xuid Oct 01 '14 at 06:01
  • Forget about `bin`. You don't need to worry about that. That is for the IDE's view. Just worry about the `src`, that is _your_ view. When the IDE runs your program, it should copy all the files in the `src` into the `bin` and compile all your .java files to .class files and put them in the bin also. – Paul Samsotha Oct 01 '14 at 06:03
  • So from the IDE view, you should be basically seeing `Project/src/res/theimage.png` and using `getResource("/res/theimage.png");`. Clean and build your project if you need to. Also try creating a new project and trying. – Paul Samsotha Oct 01 '14 at 06:05
  • I am trying to be as specific as possible to avoid misunderstanding, sorry if the text becomes too long. I put theimage.png in res and put res in src. Changed imgpath = "/res/theimage.png" still returning null. This is in a new project. – Xuid Oct 01 '14 at 06:08
  • Are you using an IDE? – Paul Samsotha Oct 01 '14 at 06:10
  • Sorry for the late response, I am using Eclipse. – Xuid Oct 01 '14 at 06:19
  • OK create a new Java Project. Create a `res` package in the `src`. Add the image the `res`. Create a package for your class. Just create one simple class, and call the code we've been talking about. Then run the application. If that works, then check your original project to see what you may be doing differently. If it doesn't work, I don't know what to tell you. This always works for me – Paul Samsotha Oct 01 '14 at 06:22
  • See [images and example](http://stackoverflow.com/a/25636097/2587435) – Paul Samsotha Oct 01 '14 at 06:35
  • Thank you for at least trying, if I ever figure it out i'll try to post it here. – Xuid Oct 01 '14 at 07:01

3 Answers3

1

I had the same issue on my mac using spring boot :

the file is located on properties/report/example.jasper

when the path was : "report/example.jasper" i got nullPointerException

So i changed to : "./report/example.jasper" and It works fine without any bug.

  InputStream inStream = null;
    try {
        inStream = ExportController.class.getClassLoader().getResourceAsStream(path);
        final JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inStream);
        jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
        jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
    } catch (final JRException jre) {
        throw new TechnicalException("Error when export jasper");
    } finally {
        if (inStream != null) {
            inStream.close();
        }
    }
0

you get nullpointer exception because there is no image named imgpath in that folder

public class SampleClass
{
    static String imgpath = "/theimage.png";

    public static void main(String[] args)
    {
        System.out.println(imgpath);
        System.out.println(SampleClass.class.getResource(imgpath));
        try
        {
            BufferedImage image = ImageIO.read(SampleClass.class.getResource(imgpath));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

}
Piumi Wandana
  • 220
  • 1
  • 5
  • 14
0

I faced the same issue on Mac. Here how I now get files from resources. For example, I have a common Maven project with resource folder in src/main. In resource folder I have a file "test.txt".

To get a path to the file:

public class Utils {
    public static String getFilePathInResources() {
        URL url = Utils.class.getClassLoader().getResource("test.txt");
        return url.getPath();
    }
}

Here the filename is hardcored just for clearity, of course, really it is a parameter in the method.

If set a filename as "/test.txt" with "/" - this will give null.

URL url = Utils.class.getClassLoader().getResource("/test.txt"); // url == null
Kirill Ch
  • 5,496
  • 4
  • 44
  • 65