Search code examples
javajava-memidplcdui

Loading image in Java J2ME


I have a problem with loading image with java 2ME. I have a image file "picture.png" in location drive "C:". After that I wrote my like this to show image from this location.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class ImageMidlet extends MIDlet implements CommandListener{
    private Display display;
    private Command exitCommand;
    private Command backCommand;
    private Command okCommand;
    private Form form;

    private ImageItem imageItem;
    private Image image;

    public ImageMidlet(){
        display = Display.getDisplay(this);
        form=new Form("");
        exitCommand = new Command("Exit", Command.EXIT, 1);
        backCommand = new Command("Back", Command.BACK, 2);
        okCommand = new Command("OK", Command.OK, 3);

        try {
            image=Image.createImage("/picture.png");
            imageItem=new ImageItem(null,image,ImageItem.LAYOUT_NEWLINE_BEFORE,"");
        }
        catch(IOException ex){

        }
        form.append(imageItem);
        form.addCommand(okCommand);
        form.addCommand(exitCommand);
        form.addCommand(backCommand);
        form.setCommandListener(this);

        display.setCurrent(form);

    }

    public void commandAction(Command c,Displayable d){

    }

    public void startApp() {
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

It shows me this error:

Unable to create MIDlet Test.ImageMidlet
java.lang.NullPointerException
     at javax.microedition.lcdui.Form.append(Form.java:638)
     at Test.ImageMidlet.<init>(ImageMidlet.java:39)
     at java.lang.Class.runCustomCode(+0)
     at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
     at com.sun.midp.midlet.Selector.run(Selector.java:151)

I am starting learn to develop, so please guide to do this.


Solution

  • As msell said - You can't access images from Your computer. Make sure that You have included the given image in midlet jar file. If You try to access it using '/picture.png', then it should be located a the root directory in jar.