tags:

views:

2072

answers:

7

Hi all,

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.

Ex: 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.(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.

Thanks, Sopolin

A: 

My guess is that

image=Image.createImage("/picture.png");

throws an exception which prevents the creation of a new object of type ImageItem which leaves your imageItem variable as null. This gives you the null pointer exception.

Isn't your file Picture.png and not Pictur.png?

Prashast
I wrote the wrong word as you mention. But it can't load this image. It still shows this error.
Sopolin
A: 

Verify that the file picture.png actually exists

depending on the device emulator/IDE there should be a way to set the "HOME" directory for the device. In your case, this would be "C:\"

+2  A: 

Image.createImage(String name) loads the given image as a resource. Resources are loaded with Class.getResourceAsStream(name), which looks up the resources from classpath, not from your file system root.

You should put the image file in your classpath, which is usually the final application .jar file. Usually a folder called resources or res is created under the project, where the images are placed. The contents of this folder are then copied to the .jar file. In development phase you should be able to append your resource folder to the classpath with a command-line parameter (java -cp resources in Java SE) or with a similar IDE setting.

If you are really interested in loading the images from actual file system, you can use optional FileConnection API (http://developers.sun.com/mobility/apis/articles/fileconnection/). The handset support for this API is limited though. For static images the classpath is the way to go.

msell
Hi msell, could you guide me put the image file in classpath and the sample code how to get it. Regarsds, Sopolin
Sopolin
+1  A: 

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.

JaanusSiim
Hi JaanusSiim, I know what you said, but I don't how to set location of the root directory in jar. Could you guide me set this? Thanks, Sopolin
Sopolin
What tool are You using to create the jar? Maybe the image is already there (some tools put images to 'res' folder and You would need to use '/res/picture.png'). Maybe You just need to correct the path. And root directory is in no subfolder :)
JaanusSiim
Hi, I am using NetBeans v6.7.1. Reply me soonThanks,Sopolin
Sopolin
With platform Java_ME_platform_SDK_3.0
Sopolin
Thank you very much JaanusSiim, I understand it.
Sopolin
A: 

am also getting the same error while trying to load an image!!how to append resource folder to class path?

Hi chinnu, I find this solution. You just only create one folder in your project. And then copy the file picture to this folder and you run like my code. It can work.
Sopolin
A: 

check the filename is correct. Image loader is case sensitive and even the file name "picture.Png", you will get the exception...

Mutlu
A: 

First of all place your image in default package. I have placed "My Network Places.png" in default package. Then create MIDlet named "ImageItemExample" then copy below code in that MIDlet file. . import java.io.; import javax.microedition.midlet.; import javax.microedition.lcdui.*;

public class ImageItemExample extends MIDlet implements CommandListener{ private Display display; private Command exit; private Form form; private ImageItem logo;

public ImageItemExample(){ form = new Form("Image Item"); exit = new Command("Exit", Command.EXIT, 0); try{ logo = new ImageItem(null, Image.createImage("/My Network Places.png"), ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_BEFORE | ImageItem.LAYOUT_NEWLINE_AFTER, "Roseindia"); form.append(logo); }catch(IOException e){ form.append(new StringItem(null, "Roseindia: Image not available: "+ e)); } }

public void startApp(){ display = Display.getDisplay(this); form.addCommand(exit); form.setCommandListener(this); display.setCurrent(form); }

public void pauseApp(){}

public void destroyApp(boolean unconditional){ notifyDestroyed(); }

public void commandAction(Command c, Displayable d){ String label = c.getLabel(); if(label.equals("Exit")){ destroyApp(true); } } }

Vikesh