Question

Hi i want to show image list with string in j2me and below is my code

enter image description here

   public ListImage() {
    try {
        for (int i = 0; i < 2; i++)
            img[i] = Image.createImage("/res/flag_" + i + ".png");

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

Here u can see that image is in res folder and i am accessing image with /res/flag_ but i still get an error of Null Pointer Exception

  Using Untrusted simulated domain
 Cannot read /res/flag_0.png
java.lang.NullPointerException
 at ListImage.startApp(+12)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
 null
null
null
java.lang.NullPointerException
at ListImage.startApp(+12)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
null
Was it helpful?

Solution

In J2ME, res is the Resource Directory for any image, data, etc files. It's path is root (/) by default. And hence not required to use it's name in the path /res/MyImage.png. Instead call it as /MyImage.png.

Example:

Image img = Image.createImage( "/MyImage.png" );  

Refer to:

  1. Developing J2ME applications with EclipseME
  2. J2ME Tutorial: User Interfaces with MIDP 2.0
  3. Where do I put images in J2me application
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top