×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Unhandled exception in j2me

    I m developing location based services in mobile phone .The application on client side obtains network parameters and then retrieves corresponding location stored in database on server. Then using this location it displays user location on map using google api.It shows correct output when run on s60 series GPRS enabled nokia phone but sometimes it gives unhandled exceptoin. I am unable to figure out what is wrong with the code.Please help me

    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.ContentConnection;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;

    public class HelloWorld extends MIDlet implements CommandListener{
    private Form form;
    private Display display;
    private Command exit = new Command("Exit", Command.EXIT, 1);

    String url = "";
    private String S60_cell_id;
    private String Signal;
    private Form form1;
    private Command cmExit;
    private Command cmView;
    private Form fmViewPng;
    String loc = null;
    public HelloWorld(){
    super();
    }



    public void startApp(){
    form = new Form("Hello World");
    String msg = "Hello World!!!!!!!";
    form.append(msg);
    form.addCommand(exit);
    cmView = new Command("View", Command.SCREEN, 2);
    form.addCommand(cmView);
    display = Display.getDisplay(this);
    display.setCurrent(form);
    S60_cell_id = System.getProperty("com.nokia.mid.cellid");
    Signal = System.getProperty("com.nokia.mid.networksignal");
    // S60_cell_id="1351";
    // Signal="78";
    url = "http://locateme.co.in/welcome.php?cellid="+S60_cell_id+"&signal="+Signal;
    form.setCommandListener(this);
    connection(url);


    }

    public void pauseApp(){}

    public void exit(){
    destroyApp(false);
    notifyDestroyed();
    }

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



    private void connection(String url) {
    StreamConnection sc = null;
    InputStream is = null;
    StringBuffer buffer = new StringBuffer();

    try {

    sc = (StreamConnection) Connector.open(url);
    is = sc.openInputStream();
    int chars;
    while ((chars = is.read()) != -1) {
    if ((chars != ' ')&&(chars!='\n')) {
    buffer.append((char) chars);
    }
    }
    System.out.println(buffer.toString());
    loc = buffer.toString();
    form.append(loc);



    } catch (IOException ex1) {
    ex1.printStackTrace();
    }

    finally{
    if(is != null){
    try {
    is.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    if(sc != null){
    try {
    sc.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }


    }

    public void commandAction(Command c, Displayable d) {
    if (c == exit) {
    exit();
    }
    if (c == cmExit)
    {
    destroyApp(false);
    notifyDestroyed();
    }
    else if (c == cmView)
    {
    int width = form.getWidth();
    int height = form.getHeight();
    url ="http://maps.google.com/maps/api/staticmap?center="+loc+"&format=png32&zoom=14&size="+width+"x"+height+"&markers=color:blue|label:S|"+loc+"&sensor=true&key=ABQIAAAAe-XrGezTSfvomiYHOAdjexSrqYa9FVv0ZzmTKrWlYncjiNW5DhSw7OuYiFstDeXcRsMloWpJpsiXrQ";
    // url = "http://maps.google.com/staticmap?center=" + loc + "&format=png32&zoom=15&size="+width+"x"+height + "&key=ABQIAAAAiyHApDe39izCxX3VkLY8jhSrqYa9FVv0ZzmTKrWlYncjiNW5DhSt_6ACgssm3qmm3HiM6dN0jctQhQ";

    cmExit = new Command("Exit", Command.EXIT, 1);


    // Create the form that will hold the png image
    fmViewPng = new Form("");
    display.setCurrent(fmViewPng);

    fmViewPng.addCommand(cmExit);

    // Set up a listener for form
    fmViewPng.setCommandListener(this);
    // Download image and place on the form
    try
    {
    Image im;

    if ((im = getImage(url)) != null)
    {
    ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);

    // If there is already an image, set (replace) it
    if (fmViewPng.size() != 0)
    fmViewPng.set(0, ii);
    else // Append the image to the empty form
    fmViewPng.append(ii);
    }
    else
    fmViewPng.append("Unsuccessful download.");

    // Display the form with the image
    display.setCurrent(fmViewPng);
    }
    catch (Exception e)
    {
    System.err.println("Msg: " + e.toString());
    }


    }

    }

    private Image getImage(String url) throws IOException
    {
    ContentConnection connection = (ContentConnection) Connector.open(url);

    // * There is a bug in MIDP 1.0.3 in which read() sometimes returns
    // an invalid length. To work around this, I have changed the
    // stream to DataInputStream and called readFully() instead of read()
    // InputStream iStrm = connection.openInputStream();
    DataInputStream iStrm = connection.openDataInputStream();

    ByteArrayOutputStream bStrm = null;
    Image im = null;

    try
    {
    // ContentConnection includes a length method
    byte imageData[];
    int length = (int) connection.getLength();
    if (length != -1)
    {
    imageData = new byte[length];

    // Read the png into an array
    // iStrm.read(imageData);
    iStrm.readFully(imageData);
    }
    else // Length not available...
    {
    bStrm = new ByteArrayOutputStream();

    int ch;
    while ((ch = iStrm.read()) != -1)
    bStrm.write(ch);

    imageData = bStrm.toByteArray();
    bStrm.close();
    }

    // Create the image from the byte array
    im = Image.createImage(imageData, 0, imageData.length);
    }
    finally
    {
    // Clean up
    if (iStrm != null)
    iStrm.close();
    if (connection != null)
    connection.close();
    if (bStrm != null)
    bStrm.close();
    }
    return (im == null ? null : im);
    }
    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Unhandled exception in j2me

    Can we continue this in your original thread? I'm assuming it's yours, or someone else has written identical code...

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Re: Unhandled exception in j2me

    The other person is my project partner.. its our be project

Similar Threads

  1. Unhandled Exception, close application error when trying hello world ...
    By nokiauser22 in forum Mobile Java Tools & SDKs
    Replies: 9
    Last Post: 2010-12-09, 15:24
  2. Unhandled exception type PIMException problem
    By pornchaispencer in forum Mobile Java General
    Replies: 4
    Last Post: 2008-06-10, 17:09
  3. Unhandled exception at
    By phoneynk in forum Symbian
    Replies: 2
    Last Post: 2007-03-12, 15:48
  4. Unhandled exception in EPOC.EXE
    By gark in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2004-05-27, 10:24
  5. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×