Question

I am trying to implement jsr-179 APi into Nokia Symbian phone for periodic location update using setLocationListener through J2me. In emulator it is working fine. While I installed Midlet on the device nokia 5230, it is given NullPointerException and the application is automatically terminating. What might be possible causes?

Below is my class, I am instantiating object for this class on a form in netbeans

class MovementTracker implements LocationListener {

        LocationProvider provider;
        Location lastValidLocation;
        UpdateHandler handler;
        boolean done;

    public MovementTracker() throws LocationException
    {

    done = false;
    handler = new UpdateHandler();
    new Thread(handler).start();

        //Defining Criteria for Location Provider
       /*
        Criteria cr = new Criteria();
        cr.setHorizontalAccuracy(500);
       */


        //you can place cr inside getInstance
    provider = LocationProvider.getInstance(null);

        //listener,interval,timeout,int maxAge
        //Passing -1 selects default interval
       // provider.setLocationListener(MovementTracker.this, -1, -1, -1);
         provider.setLocationListener(MovementTracker.this, -1, 30000, 30000);

    }

    public void locationUpdated(LocationProvider provider, Location location)
    {
    handler.handleUpdate(location);
        batteryLevel = System.getProperty("com.nokia.mid.batterylevel");
        sn = System.getProperty("com.nokia.mid.networksignal");
        localTime =  System.currentTimeMillis();

        Send_Location();
    }

    public void providerStateChanged(LocationProvider provider, int newState)
    {
    }

    class UpdateHandler implements Runnable
    {
    private Location updatedLocation = null;

    // The run method performs the actual processing of the location

    public void run()
        {
        Location locationToBeHandled = null;
        while (!done)
            {
        synchronized(this)
                {
            if (updatedLocation == null)
                    {
            try
                        {
                wait();
            }
                        catch (Exception e)
                        {
                // Handle interruption
            }
            }
            locationToBeHandled = updatedLocation;
            updatedLocation = null;
        }

        // The benefit of the MessageListener is here.
        // This thread could via similar triggers be
        // handling other kind of events as well in
        // addition to just receiving the location updates.
        if (locationToBeHandled != null)
            processUpdate(locationToBeHandled);
        }
                try
                {
                    Thread.sleep(10000); //Sleeps for 10 sec & then sends the data
                }
                catch (InterruptedException ex)
                {
                }


    }

    public synchronized void handleUpdate(Location update)
        {
        updatedLocation = update;
        notify();
    }

    private void processUpdate(Location update)
        {
        latitude = update.getQualifiedCoordinates().getLatitude();
            longitude = update.getQualifiedCoordinates().getLongitude();
            altitude = update.getQualifiedCoordinates().getAltitude();

    }
    }
} 
Était-ce utile?

La solution

public MovementTracker() throws LocationException

...
I have not written any code for handling LocationException.

No code is very dangerous practice, just search the web for something like "java swallow exceptions".

It is quite possible that because of implementation specifics Nokia throws LocationException where emulator does not throw it. Since you don't handle exception this may indeed crash you midlet at Nokia - and you wouldn't know the reason for that because, again, you have written no code to handle it.

How can I catch that exception?

The simplest thing you can do is to display an Alert with exception message and exit the midlet after user reads and dismisses alert

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top