Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 39
Registered: ‎06-23-2009
My Device: Storm 9530
Accepted Solution

NullpointerException in LocationProvider Listenerthread

Ive been hunting around the forum for a while for a solution to this problem but have had no luck.... ive developed an app that needs to monitor gps location... using the obvious locationprovider with autonomous criteria i get it working.... for the first 5 minutes at least... my debugging code shows me that the location gets updated from the provider (added locationlistener) regularly and without any hickups... however after about 5 mins or so (and yes this is consistent) i get a nullpointer exception... using the debugger in eclipse it shows that is is coming from LocationProvider ListenerThread.run at line 644... obviously i cannot see what it is doing so this is where my head aches come in... below is my code (although not very complicated) that i am using to make everything work... any input is helpful but ill keep working on this... if i am missing something obvious please feel free to point me in the right direction (or link for that matter) thanks....

 

 

public class GPS implements LocationListener { private LocationProvider provider; private Location location; private WhereAreYouMainScreen mainscreen; public GPS(Criteria criteria,WhereAreYouMainScreen main) throws LocationException { this.provider=LocationProvider.getInstance(criteria); this.provider.setLocationListener(this, -1, -1, -1); this.mainscreen=main; } public Location getLocation(RichTextField rich) { return this.location; } public void locationUpdated(LocationProvider provider, Location location) { System.out.println("Location updated new location: "+location.getQualifiedCoordinates().getLatitude()+" "+location.getQualifiedCoordinates().getLongitude()); this.mainscreen.LocationUpdated(location); this.location=location; } public void providerStateChanged(LocationProvider provider, int newState) { System.out.println("Provider state changed"); }

 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: NullpointerException in LocationProvider Listenerthread

Have you used device debugger? Check which object is getting null and when on real device.

Use Search. "Accept Solution" If the problem is resolved.
Highlighted
Developer
Posts: 293
Registered: ‎02-25-2009
My Device: Not Specified

Re: NullpointerException in LocationProvider Listenerthread

In LocationUpadated() method, you are directly printing latitude and longitude using location object. In case when GPS is not being able to give location object , calling location.getQualifiedCoordinates.getLatitude() will lead to NullPointerException.

 

please put a chk , before these print statements, something like if(location != null) ,

 

 

Thanks

Amardeep

www.impetus.com

Developer
Posts: 39
Registered: ‎06-23-2009
My Device: Storm 9530

Re: NullpointerException in LocationProvider Listenerthread

thanks for the quick responses... ive added the check and some debugging system.out.... heres what i have been noticing when i enter the locationupdated method eveything works fine but as soon as i go to check whether the location object is null thats where the program hangs... now the simulator just freezes and doesnt report any issues but just sits there.. heres the check i added let me know if i should add something else...

 

i also had a couple questions if you wouldnt mind shedding some light on.... you suggested to check whether the location object was null... however if the location is updated and the provider state doesnt change to unavailable or temporary unavailable why would the location object return null? i have a print statement to tell me when the provider state changes and the state shouldnt change on the simulator since im not changing any of the satellite settings...

 

im starting to think that this might be an issue with the thread monitor.... since my code is trying to access an object passed into it (the location object) before my code "officially has the monitor"... however shouldnt the monitor be given up before the provider calls the locationupdated method...

 

thanks for you help much appreciated...

public void locationUpdated(LocationProvider provider, Location location) { System.out.println("LOcation updated check validity"); if(location!=null) { System.out.println("New Valid Location: "+location.getQualifiedCoordinates().getLatitude()+" "+location.getQualifiedCoordinates().getLongitude()); this.mainscreen.LocationUpdated(location); return; } System.out.println("Error Location was NULL"); }

 

Developer
Posts: 39
Registered: ‎06-23-2009
My Device: Storm 9530

Re: NullpointerException in LocationProvider Listenerthread

ok so i think ive solved the problem... i was trying to use if(location!=null) but the program kept hanging at that one spot... looking through the location class i found isvalid()... this seems to work but i cannot explain why the provider would say that it is avaailable as i check the provider state from inside the locationUpdated() method and the provider is always avaiable... heres my code i hope if n e one runs into this problem that this helps...

public void locationUpdated(LocationProvider provider, Location location) { System.out.println("LOcation updated check validity"); if(location.isValid()) { System.out.println("New Valid Location: "+location.getQualifiedCoordinates().getLatitude()+" "+location.getQualifiedCoordinates().getLongitude()); this.mainscreen.LocationUpdated(location); return; } System.out.println("Error Location was NULL"); int i=provider.getState(); if(i==LocationProvider.AVAILABLE) { System.out.println("Weird provider was avaialable"); } if(i==LocationProvider.OUT_OF_SERVICE) { System.out.println("Provider out of servcie"); } if(i==LocationProvider.TEMPORARILY_UNAVAILABLE) { System.out.println("Provider temp unavailable"); } }