2

I am kinda new to BlackBerry OS development, my code is trying to get name of the city while using LocationProvider after passing Criteria parameter. i was following according to this link from RIM itself i tried "JSR-179" and "JSR-179 Extension" My code is as follows:

"with JSR 179"

public String CurrentLocation() 
    {
        try 
        {
            //LBS(Location Based Service) JSR-179
            Criteria criteria = new Criteria();
            criteria.isAddressInfoRequired();
            criteria.setCostAllowed(false);
            criteria.setAddressInfoRequired(true);
            criteria.setHorizontalAccuracy(200);
            criteria.setVerticalAccuracy(200);

            locationProvider = LocationProvider.getInstance(criteria);

            location = locationProvider.getLocation(10);
            cityName = location.getAddressInfo().getField(AddressInfo.CITY);
        } 
        catch (LocationException le) 
        {
            new Tracer("CurrentLocation() caught LocationException : " + le.getMessage());
            le.printStackTrace();
        }
        catch (InterruptedException ire) 
        {
            new Tracer("CurrentLocation() caught InterruptedException: " + ire.getMessage());
            ire.printStackTrace();
        }
        return cityName;
    }

with JSR-179 Extension

public String CurrentLocaiton(){
        try
        {
            BlackBerryCriteria BBCriteria = new BlackBerryCriteria();
            BBCriteria.setAddressInfoRequired(ProvideAddressInfoTExt);
            BBCriteria.setAltitudeRequired(true);
            BBCriteria.setSatelliteInfoRequired(true, true);
            BBCriteria.setCostAllowed(true);
            BBCriteria.setPreferredPowerConsumption(BlackBerryCriteria.POWER_USAGE_HIGH); //testing for high power

            BlackBerryLocationProvider bbLocationProvider 
                                                = (BlackBerryLocationProvider) 
                                                    BlackBerryLocationProvider.getInstance(BBCriteria);
            location = bbLocationProvider.getLocation(9000);
            QualifiedCoordinates qualCoor = location.getQualifiedCoordinates();
            cityName = location.getAddressInfo().getField(AddressInfo.CITY);
        }
        catch(LocationException le)
        {
            le.printStackTrace();
            System.out.println(le.getMessage());
        }
        catch(InterruptedException ie)
        {
            ie.printStackTrace();
            System.out.println(ie.getMessage());
        }
        catch(NullPointerException npe)
        {
            npe.printStackTrace();
            cityName = "Caught Null Pointer";
            Dialog.alert(npe.getMessage());
        }
        return cityName;
    }

Am I missing something there or is there something wrong I have been scratching on this issue for hours now. I tried to catch Null Pointer Exception from this code snippet as well and if catching from "static void main" then Uncaught Exception: pushModalScreen called by a non-event thread

        ....
        try{
            String location = cellLocation.CurrentLocation();
            LabelField towerlocationText = new LabelField(towerString + location,
                                LabelField.FOCUSABLE|LabelField.DEFAULT_POSITION);
            add(towerlocationText);
        }
        catch(NullPointerException npe)
        {
            npe.printStackTrace();
            System.out.println(npe.getMessage());
            Dialog.alert(npe.getMessage() + "" + "AddresInfo/LocationProvider/Locaiton gave null pointer exception" );
        }
        ...

tried on BB-9700 BB-9790 Devices as well on Simulator BB-9900

Thanks,

Rohan
  • 53
  • 5
  • 1
    `Dialog.alert()` is not a good way to log exceptions. For your case it throws "pushModalScreen called by a non-event thread". Use `Logger` class or a text file on the device memory/sd card to log information you want. Regarding your code, seems that cellLocation.CurrentLocation() returns null value. And it is the reason of NPE in your case. Make sure you have GPS sattelites available when running this code on actual device and use Logger class to log every `suspicious` steps you need to make. To view log on a device with keyboard, hit CTRL, keep it pressed and hit `LGLG` on the keyboard. –  Oct 05 '12 at 05:13

1 Answers1

0
use 
Either :- 
/***/
      UiApplication.getUiApplication().invokeLater(new Runnable() 
        {
            public void run() 
            {
                Dialog.alert("Hello");
            }
        });
/***/
or      
        synchronized (UiApplication.getEventLock()) 
        {
                    Dialog.alert("Hello");
        }

wherever you are trying to show Dialog on Non-event thread , you should need a Event Lock. You can use above method to make Event Lock read the article for more info .

AK Joshi
  • 877
  • 6
  • 20