×

Discussion Board

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

    getLocation always throws NullPointerException

    hey guys,
    i try to code my way through a gps locator. after reading tons of tutorials i couldn't find any more different in my code than in the tutorial
    anyway, my problem is, that my application always gives me a nullpointerexception! after hours troubleshooting i still can't find the error!
    i desperately need a solution!!
    i put some sysouts in my code to track down the problem and i found out, that it starts right after the try-statement.

    PHP Code:
    lp LocationProvider.getInstance(criteria); ---> always returns null! (?) 
    do you have any idea what could be the problem???
    please help!

    this is my code:

    PHP Code:
    package locator;

    import javax.microedition.location.*;

    class 
    Locator {
        public final static 
    Locator INSTANCE = new Locator();

        private 
    GeoAddress mostRecentGeoAddress null;
        private 
    Criteria criteria null;
        private 
    LocationProvider lp null;
        
        protected 
    Locator() {
            
    mostRecentGeoAddress = new GeoAddress(00);
            
    // Create a Criteria object for defining desired selection criteria
            
    criteria = new Criteria();
            
    // Specify horizontal accuracy of 500 meters, leave other parameters 
            // at default values.
            
    criteria.setHorizontalAccuracy(25);
            
    criteria.setVerticalAccuracy(25); // 25m
            
    criteria.setPreferredResponseTime(Criteria.NO_REQUIREMENT);
            
    criteria.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
            
    criteria.setCostAllowed(false); 
            
    criteria.setSpeedAndCourseRequired(true);
            
    criteria.setAltitudeRequired(true);
            
    criteria.setAddressInfoRequired(true);

        }
        public 
    GeoAddress getCurrentGeoAddress() {
            
    mostRecentGeoAddress null;
            
    Coordinates c null;
            
    Location l null;
            try {
                
    lp LocationProvider.getInstance(criteria);
                
                
    System.out.println("Location Provider: "+lp);
                
    //            get the location, 60s timeout
                
    lp.getLocation(60);
                
    System.out.println("Got Location: "+l);
                
    l.getQualifiedCoordinates();
                
    System.out.println("Qualified: "+c);
            
                if (
    != null) {
                    
    c.getLongitude();
                    
    c.getLatitude();
                    
    mostRecentGeoAddress = new GeoAddress(c.getLongitude(), c.getLatitude());
                    
    System.out.println(c.getLongitude()+", "+c.getLatitude());
                }
            } catch (
    LocationException e) {
                
    // TODO Auto-generated catch block
                
    e.printStackTrace();
            } catch (
    InterruptedException e) {
                
    e.printStackTrace();
            }

            return 
    mostRecentGeoAddress;
        }
        
        public 
    GeoAddress getostRecentGeoAddress() {
            return 
    mostRecentGeoAddress;
        }

    Last edited by Wurstgesicht; 2009-03-19 at 16:00. Reason: Edit: I am testing it with the S60 and on a Nokia N95

  2. #2
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    198

    Re: getLocation always throws NullPointerException

    I suspect that your criteria are too strict. Try using

    Code:
    lp = LocationProvider.getInstance(null);
    Just to make sure that you at least get any location provider.

    Then try adding the criteria specifications back in again one by one until you find the one that returns the null provider.

    FYI... it's probably

    Code:
    criteria.setAddressInfoRequired(true);
    But it may also be

    Code:
    criteria.setCostAllowed(false);
    - Mike
    NAVTEQ Network for Developers
    The community for developing innovative location-based applications
    http://NN4D.com

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Re: getLocation always throws NullPointerException

    hey mike,
    thanks for your reply!
    You were right! The criteria was too strict. So I changed it.

    Now it works fine in the WTK Emulator! When I enter a location, it will be displayed in the emulator.

    But when I upload it on the Nokia N95 it is still throwing me an UnhandledException!!!

    Do you have an idea, why it can't work on the N95?
    I wrote a lot of debug labels to print out the error, but it is not even starting the main form?!

    My code:

    PHP Code:
    package locator;

    import javax.microedition.location.Coordinates;
    import javax.microedition.location.Criteria;
    import javax.microedition.location.Location;
    import javax.microedition.location.LocationProvider;

    import com.sun.lwuit.Form;
    import com.sun.lwuit.Label;


    class 
    Locator {
        public final static 
    Locator INSTANCE = new Locator();

        private 
    GeoAddress mostRecentGeoAddress null;
        private 
    LocationProvider lp null;
        private 
    Criteria criteria null;
        protected 
    Locator() {
            
    mostRecentGeoAddress = new GeoAddress(00);
            
    // Create a Criteria object for defining desired selection criteria
            
    criteria = new Criteria();
            
    // Specify horizontal accuracy of 500 meters, leave other parameters 
            // at default values.
            
    criteria.setHorizontalAccuracy(25);
            
    criteria.setVerticalAccuracy(25); // 25m
            
    criteria.setPreferredResponseTime(Criteria.NO_REQUIREMENT);
            
    criteria.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
            
    criteria.setCostAllowed(true); 
            
    criteria.setSpeedAndCourseRequired(true);
            
    criteria.setAltitudeRequired(true);
            
    criteria.setAddressInfoRequired(true);

        }
        public 
    GeoAddress getCurrentGeoAddress() {
            
    Form f = new Form("Locator Debug");
            
    mostRecentGeoAddress null;
            
    f.addComponent(new Label("App started!"));
            
            try {
                
                
    f.addComponent(new Label("LocationProvider State: "+lp+". Now get new instance!"));
                
                
    lp LocationProvider.getInstance(criteria);
                
    f.addComponent(new Label("Location Provider State: "+lp.getState()));
                
                
    // get the location, 60s timeout
                
    Location l lp.getLocation(60);
                
                
    Coordinates c l.getQualifiedCoordinates();
                
                if (
    != null) {
                   
    mostRecentGeoAddress = new GeoAddress(c.getLongitude(), c.getLatitude());
                   
    f.addComponent(new Label("Longitude: "+c.getLongitude()));
                   
    f.addComponent(new Label("Latitude: "+c.getLatitude()));
                }
                
    //          debug
                
    f.show();
            } catch (
    Throwable t) {
               
    Form err = new Form("Error");
               
    err.addComponent(new Label("You could not be located!"));
               
    err.addComponent(new Label("LocationProvider State: "+lp.getState()));
               
    err.addComponent(new Label("Error Message: "+t.getMessage()));
            } 
            return 
    mostRecentGeoAddress;
        }
        public 
    GeoAddress getostRecentGeoAddress() {
            return 
    mostRecentGeoAddress;
        }

    And this is the midlet:

    PHP Code:
    package locator;

    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;

    import com.sun.lwuit.*;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;

    public class 
    LocatorTestMidlet extends MIDlet implements ActionListener {

        public 
    LocatorTestMidlet() {
            
    // TODO Auto-generated constructor stub
        
    }

        protected 
    void destroyApp(boolean arg0throws MIDletStateChangeException {
            
    // TODO Auto-generated method stub

        
    }

        protected 
    void pauseApp() {
            
    // TODO Auto-generated method stub

        
    }

        protected 
    void startApp() throws MIDletStateChangeException {
            
    Display.init(this);
            
            
    Form f = new Form("Locator Test");
            
    Label latitudeLabel = new Label();
            
    Label longitudeLabel = new Label();
            
    Label latitude = new Label();
            
    Label longitude = new Label();
            
            
    GeoAddress geoAddress null;
            
            
    double latitudeString .0;
            
    double longitudeString .0;
            
            
    geoAddress Locator.INSTANCE.getCurrentGeoAddress();    
            
            
    Command exitCommand = new Command("Exit");
            
            
    longitudeString geoAddress.getLongitude();
            
    latitudeString geoAddress.getLatitude();
            
            
    longitudeLabel.setText("Longitude: ");
            
    longitude.setText(Double.toString(longitudeString));
            
    latitudeLabel.setText("Latitude: ");
            
    latitude.setText(Double.toString(latitudeString));
            
            
    f.addCommand(exitCommand);
            
    f.setCommandListener(this);
            
    f.addComponent(longitudeLabel);
            
    f.addComponent(longitude);
            
    f.addComponent(latitudeLabel);
            
    f.addComponent(latitude);
            
            
    //f.show();
        
    }
        
        public 
    void actionPerformed(ActionEvent arg0) {
            
    notifyDestroyed();
            
        }



  4. #4
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    198

    Re: getLocation always throws NullPointerException

    Quote Originally Posted by Wurstgesicht View Post
    You were right! The criteria was too strict. So I changed it. Now it works fine in the WTK Emulator! When I enter a location, it will be displayed in the emulator. But when I upload it on the Nokia N95 it is still throwing me an UnhandledException!!!
    Just because it works in the emulator does NOT mean it will work on the device. In particular, I strongly suspect that your criteria specification for addressInfoRequired(true) means that the N95 has no provider to give you.

    - Mike
    NAVTEQ Network for Developers
    The community for developing innovative location-based applications
    http://NN4D.com

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

    Re: getLocation always throws NullPointerException

    hmmm... i don't understand it. i removed the whole criteria to try with

    Code:
    lp = LocationProvider.getInstance(null);
    then i tried step by step with various criterias.
    after every change i created a package, deleted the one on the phone and upload it on my N95. all the time the damn same
    "unhandled exception error"...
    probably i have missed something?!

  6. #6
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    198

    Re: getLocation always throws NullPointerException

    Quote Originally Posted by Wurstgesicht View Post
    hmmm... i don't understand it. i removed the whole criteria to try with

    Code:
    lp = LocationProvider.getInstance(null);
    then i tried step by step with various criterias.
    after every change i created a package, deleted the one on the phone and upload it on my N95. all the time the damn same
    "unhandled exception error"
    Bear in mind that the LocationProvider.getInstance() returning null is what it is supposed to do if it can't find a provider that meets your criteria, so you should always be checking for a null return value in any case.

    What you do know if you get a null is that there are LocationProviders available, just not one that met your criteria. i.e. you are asking the N95 for something it can't provide.

    The only criteria I've ever actually used on an N95 is setSpeedAndCourseRequired(true) and I know that works. e.g.

    Code:
    Criteria criteria = new Criteria();
    criteria.setSpeedAndCourseRequired(true);
    
    LocationProvider lp = LocationProvider.getInstance(criteria);
    
    if (lp == null) {
        // No location provider available for required criteria!
    }
    - Mike
    NAVTEQ Network for Developers
    The community for developing innovative location-based applications
    http://NN4D.com

Similar Threads

  1. JSR-179 : serious bug in getLocation implementation
    By sgreenhill in forum Mobile Java General
    Replies: 8
    Last Post: 2010-08-03, 14:12
  2. Using RecordStore.AUTHMODE_ANY throws NullPointerException
    By sgaber in forum Mobile Java General
    Replies: 10
    Last Post: 2007-12-18, 18:03
  3. recordcontrol.setStream(out) throws NullPointerException
    By bharatdevada in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2006-11-10, 01:16
  4. Replies: 2
    Last Post: 2004-11-17, 10:20
  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
  •