×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Unhandled Exception

    Hi,
    I have been trying to build a location based application. For tht i hav used network parameters and gt the location frm a stored database of n/w parameters and their corresponding positions. Sometimes the application runs correctly bt sometimes i get an unhandled exception.
    I am attaching the code.

    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

    I'm afraid I can't spot code that throws an exception without some clues. Do you not have any details of the exception? Such as, which exception?

    I can tell you that you will have problems with this code, as you perform connections inside events. Event methods (like startApp() and commandAction()) need to return as quickly as possible, since the entire event mechanism is blocked while an event is being handled. Use a Thread to perform any time consuming action.

    Try to find out more about the exception. Can you reproduce the problem in an emulator? If not, add some try..catch blocks to see what you can catch.

    Graham.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Cochin, Kerala, India
    Posts
    84

    Re: Unhandled Exception

    Can u pls specify that at what stage this exception is caught? ie, when clicking any commands or anything....

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Re: Unhandled Exception

    Graham, thanks a lot...
    we will try it with threads.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Re: Unhandled Exception

    vinuk,
    the exception comes as soon as i start the application and sometimes the application runs correctly.

Similar Threads

  1. [N95] Unhandled Exception caused by using try/catch
    By jeroenoosterlaar in forum Mobile Java General
    Replies: 7
    Last Post: 2010-08-18, 05:12
  2. s60 3rd ed emulator crash
    By Kimau in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2008-06-18, 22:12
  3. VS.Net 2003 Carbide 2.01 and epoc32.exe
    By ValentinK in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 2
    Last Post: 2007-01-12, 12:31
  4. Throwing unhandled exception
    By venkat512 in forum Mobile Java Tools & SDKs
    Replies: 4
    Last Post: 2006-08-24, 08:03
  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
  •