codenameone

Cannot get resources


I am trying to read a image from the resources directory using

new DataInputStream(Display.getInstance().getResourceAsStream(FilePack.class, "/resources/0.png"));

but the InputStream is null and throws the following error message

resources cannont be nested in directories in Codename One! Invalid resource: : /resources/0.png

The path of the resources folder is

common/src/resources and of the Main class with 0.png is common/src/main/game/Main.java

How can I fix it?


Solution

  • Place images in common/src/main/resources directory. Then change

    Display.getInstance().getResourceAsStream(FilePack.class, "/resources/0.png");
    

    to

    Display.getInstance().getResourceAsStream(FilePack.class, "/0.png");
    

    Note, the generated project from https://start.codenameone.com doesn't have resources directory by default. You should create it at common/src/main/ and place images there.