0

I am relatively new to J2ME Programming. I have been asked to built a program which displays 5 fruit names, and when "show" is clicked,it displays the respective images. Although I have coded it right, but image is not displayed, all I get is a black screen on the next form.

CODE :

import javax.microedition.lcdui.* ;
import javax.microedition.midlet.*;
/**
 * @author Ashutosh
 */
public class Midlet extends MIDlet implements CommandListener
{
    private Display display ;
    private Form f,f1 ;
    ChoiceGroup cg ;
    private Image image ;
    Command cmd = new Command("SHOW" , Command.OK , 1) ;
    Command cmd1 = new Command("BACK" , Command.BACK , 1) ;
    public Midlet()
    {
        try
        {
            image = Image.createImage("Apple.png");
        } 
        catch (Exception e)
        { 

        }
    }        
    public void startApp() 
    {
        f = new Form("Home") ;
        f1 = new Form("Show Screen") ;
        cg=new ChoiceGroup("Select Apple:",Choice.EXCLUSIVE);
        display = Display.getDisplay(this) ;
        cg.append("Apple",null) ;
        cg.append("Banana",null) ;
        cg.append("Cherry",null) ;
        cg.append("Kiwi",null) ;
        cg.append("Mango",null) ;
        f.append(cg) ;
        f.addCommand(cmd);
        f1.addCommand(cmd1);
        display.setCurrent(f);
        f.setCommandListener(this);
        f1.setCommandListener(this);
    }

    public void pauseApp() 
    {
    }

    public void destroyApp(boolean unconditional) 
    {
    }

    public void commandAction(Command c, Displayable d) 
    {
        int a = cg.getSelectedIndex() ;
        if(c == cmd)
        {
            display.setCurrent(f1);
            switch(a)
            {
                case 0 :  f1.append(image) ;
                default : System.exit(0) ;     
            }
        }
        if(c == cmd1)
        {
            display.setCurrent(f);   
        }
    }
}

Thanks in advance.

2 Answers2

0

If your image is in the same package of your midlet placed try :

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

instead of

image = Image.createImage("Apple.png");

And do not forget to put e.printStackTrace(); inside your catch block so that in case of an error occurred you can figure it out.

UPDATE : Instead of :

        display.setCurrent(f1);
        switch(a)
        {
            case 0 :  f1.append(image) ;
            default : System.exit(0) ;     
        }

try :

        switch(a)
        {
            case 0 :  f1.append(image) ;
            default : System.exit(0) ;     
        }

        display.setCurrent(f1);
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
  • I initially tried that only, but since it did not worked, I switched to this. Thanks, though. – Ashutosh Agarwal Mar 05 '16 at 15:10
  • TRACE: , Exception caught in Display class java.lang.NullPointerException at javax.microedition.lcdui.Form.append(), bci=8 - ng1.Midlet.commandAction(Midlet.java:61) – Ashutosh Agarwal Mar 05 '16 at 15:17
  • Please try with my new update ... And undo the changes we have done inside the `startApp()` – Madushan Perera Mar 05 '16 at 16:02
  • No, Now it does not display form 2 at all. Says, there is null pointer exception with f1.append(image) ; – Ashutosh Agarwal Mar 05 '16 at 16:38
  • 1
    Finally got it brother. I manually placed my images in the Source Folder. You have to place it in the "Resources" directory, created only in your IDE, in my case Netbeans. Thanks a lot for your time and help. :) – Ashutosh Agarwal Mar 05 '16 at 21:50
  • I think that depends on a setting in NetBeans. I've always used NetBeans too and I always place my images and other resources with the source files. Trying to load "Apple.png" would attempt to load that image from the folder of the class. And "/Apple.png" would be from the root folder. I've seen this "must be in res folder" many times, but never encountered such a requirement myself. – mr_lou Mar 06 '16 at 04:27
0

I found the solution. You have to place your images in the "Resources" directory, created only in your IDE.Adding the image to the source folder on the Disk is inconsequential.

The following answer by msell effectively addresses the issue. https://stackoverflow.com/a/1332470/4786292

Community
  • 1
  • 1