Loading Image in Java J2ME

I have a problem loading an image using java 2ME. I have an image file "picture.png" in the location driver "C:". After that, I wrote this to show an image from this place.

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) { } } 

He 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 to learn how to develop, so please be guided by this.

+4
source share
5 answers

As msell said, you cannot access images from your computer. Make sure you include this image in the midlet file. If you try to access it using '/picture.png', then it should be located in the root directory in the jar.

+4
source

Image.createImage (String name) loads this image as a resource. Resources are loaded with Class.getResourceAsStream (name), which searches for resources from the class path, and not from the root file system.

You should put the image file in your classpath, which is usually the final .jar application. Usually, a resource or res is created under the project where the images are placed. The contents of this folder are then copied to the .jar file. At the design stage, you must add your resource folder to the class path with a command line parameter (Java-cp resources in Java SE) or with a similar IDE parameter.

If you are really interested in downloading images from a real file system, you can use the optional FileConnection API ( http://developers.sun.com/mobility/apis/articles/fileconnection/ ). However, phone support for this API is limited. For static images, the class path is the path.

+6
source

First of all, put your image in the default package. I put "My Network Places.png" in the default package. Then create a MIDlet named "ImageItemExample" then copy the code below into this 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); } } } 
+2
source

I suppose that

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

throws an exception that prevents the creation of a new object of type ImageItem, which leaves the imageItem variable set to zero. This gives you a null pointer exception.

Not your Picture.png file, not Pictur.png?

-2
source

Make sure the picture.png file really exists

Depending on the device emulator / IDE, the method for setting the "HOME" directory for the device must be set. In your case it will be "C: \"

-2
source

All Articles