Problem Loading image in j2me.

500 pts.
Tags:
Java 1.6
MIDLET
Hi, I am creating a simple midlet in j2me and I want to load an image in it, but when I run the program in emulator there is this exception that comes every time. ."java.io.IOException - javax.microedition.lcdui.ImageDataFactory.createImageFromStream(), bci=8 - javax.microedition.lcdui.ImageDataFactory.createResourceImageData(), bci=45 - javax.microedition.lcdui.Image.createImage(), bci=8 - imageDisplay.(), bci=51 - java.lang.Class.newInstance(), bci=0 - com.sun.midp.main.CldcMIDletLoader.newInstance(), bci=46 - com.sun.midp.midlet.MIDletStateHandler.createMIDlet(), bci=66 - com.sun.midp.midlet.MIDletStateHandler.createAndRegisterMIDlet(), bci=17 - com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=27 - com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52 - com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8 - com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161 - com.sun.midp.main.MIDletSuiteLoader.main(), bci=12 " and here is my code: import java.io.IOException; import java.io.InputStream; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class imageDisplay extends MIDlet implements CommandListener{ private Display display; private Command exit; private Form form; private ImageItem logo; public imageDisplay(){ form = new Form("Image Item"); exit = new Command("Exit", Command.EXIT, 0); try{ InputStream is = getClass().getResourceAsStream("/res/tux.png"); logo = new ImageItem(null, Image.createImage("/res/tux.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)); e.printStackTrace(); } } 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); } } } I don't think there is anything wrong with my code, but still the problem is coming again an again.  I've tried is so many times but I can't find a  solution. Can anyone please help?

Software/Hardware used:
Eclipse IDE
ASKED: October 15, 2012  6:09 PM
UPDATED: October 17, 2012  9:57 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 14  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    The problem could be in the path to your image resource.  Have you tried something like this ? getClass().getResourceAsStream("/tux.png");
    65,110 pointsBadges:
    report
  • hussain22juzer
    yes i have tried thatit's not working either.I've tried changing the location of images ant then setting the path.still the same result.It seems like "IO" is not working on my system,because i've googled it so many times and every where the same method is shown...but in my system it's not working
    500 pointsBadges:
    report
  • hussain22juzer
    also it does not matter whether i write it like this: Image.createImage(" ");orI provide the path:Image.createImage("/tux.png"); same error is coming. It's like the program is not even trying to read the file
    500 pointsBadges:
    report
  • carlosdl
    Your stack trace shows that the function throwing the exception is "createImageFromStream()", not "createImage()".  Can you add this to your exception handler and let us know what you see on the console ? System.out.println("My Error message: " + e.toString()); Also, to debug the error I would add a separate exception handler for each instruction (i.e. I would put each instruction in its own try-catch block), to clearly identify the instruction causing the error.
    65,110 pointsBadges:
    report
  • hussain22juzer
    Ok now I have done wat u asked with the followind output :  "My Error message:java.io.IOException"   Also fyi i am using 'SAMSUNG SDK 1.1' as my sdk...Is this causing this exception...is this sdk isn't good enough...???
    500 pointsBadges:
    report
  • TomLiotta
    ...but when I run the program in emulator...   What emulator are you using? On what device?   ‘SAMSUNG SDK 1.1′   It's certainly worth trying a release that's a little more current. You might at least try SDK 1.1.9.   Tom
    125,585 pointsBadges:
    report
  • carlosdl
    I've never used a Samsung SDK (I have only used Nokia's), but the exception message certainly looks incomplete.  I would expect something like: java.io.IOException: Cannot read /res/tux.png  I tested your code with a Nokia SDK, and it worked well using "/tux.png" as the path to the image I agree that you should try with a newer SDK.
    65,110 pointsBadges:
    report
  • hussain22juzer
    Can you please suggest me a suitable sdk for my 64 bit machine..I've tried 'Java me sdk 3.0.5' and Java me sdk 3.2 also sun wtk 2.5.2...but they all are giving troubles with 64 bit..actually they all are for 32 bit machines...
    500 pointsBadges:
    report
  • hussain22juzer
    Now i have installed netbeans as well it is also giving me the same excception.Now i think sdk is also not a problem...
    500 pointsBadges:
    report
  • hussain22juzer
    it is like my system is never reading the argument of the method createImage that is createImage("/tux.png") and giving an io.exception...

    can this be a problem with jdk 1.7?


    500 pointsBadges:
    report
  • carlosdl
    Most 32 bit applications should work well on 64 bit systems.  I installed NetBeans 7.2 (the full version that includes a Java ME SDK) today on my 64 bit Windows 7 machine and tried your code, and it failed with the same exception.  It's odd that different IDEs/SDKs create the JAR file differently.  I tried several things, but these are the two that worth mentioning:  1) Put the image file (tux.png) in the root of the "src" subdirectory, and used "/tux.png" as the path in my code.  It worked. 2)Moved the file to the "res" subdirectory and modified the project properties to include that folder when building the solution.  It also worked without changing the path to the image in my code.  To change the project properties, right click on the project name and click "properties".  Then expand the "Build" node, and click on "Libraries & Resources", and click the "Add Folder" button that appears to the right hand side of the window, to add the "src" folder, click "Ok", and build and run the solution.  The best thing you can do to investigate this kind of issues, is opening the jar file (you can open it with WinRar or any similar program) to see if the image files are included, and the path where they are.
    65,110 pointsBadges:
    report
  • carlosdl
    BTW, I also have JDK 1.7 installed.
    65,110 pointsBadges:
    report
  • hussain22juzer
    oh it just worked i put the image file into the root of the src folder and it worked...i think i had tried it before but i was always missing something...Thank you very much...BTW can u suggest me how i can develop mobile apps with touchscreen features?...
    500 pointsBadges:
    report
  • carlosdl
    I only develop Java ME  apps for Nokia devices.  Their SDK includes packages to support touch interaction, but those are specific for Nokia phones (Symbian, Anna, Belle and Series 40).  I have always used Eclipse with Nokia SDKs, but now they have released a Nokia IDE (which is also Eclipse-based).  If you are interested, the development tools can be downloaded from here:  Nokia Developer - Series 40   And you can find lots of interesting webinars here: Nokia Developer - Java Webinars
    65,110 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following