Results 1 to 4 of 4
  1. #1
    infaddict is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Question NullPointerException converting String to double

    Hi,

    This is my first post so go easy on me :D

    I am a programmer by trade (mainframe + VB), but fairly new to Java. Anyway, I'm working on a GPS application which detects, connects and retrieves information from my bluetooth GPS unit and displays it on my Nokia N95 screen. I'm using NetBeans, developing under MIDP and have made good progress so far - I am able to detect, connect and get the info I need (thanks to kind people posting sample GPS/Bluetooth code on the web).

    My main screen is simply several StringItems, one for latitude, one for longitude, one for number of satellites etc etc. The content of the StringItem's is set every second when the GPS sends its data. I have a class controlling the GPS location information and within that class I have latitude, longitude etc as String types.

    I can run the application on the phone and it works well. Screen shows :

    Connected.
    Quality : 1
    Latitude : 5443.8845
    Longitude : 243.1131


    So far so good. I would now like to do some maths with the lat/long information, in order to calculate distance and speed.

    I have tried converting the String information into a double, but always get a NullPointerException. Some code snapshots of what I am trying to do :

    in Location.java :
    ...
    public class Location {

    // NMEA Elements
    String latitude;
    String longitude;
    ...
    ...


    in BluetoothGPSMidlet.java :
    ...
    private double dblLatitude;
    ...
    try
    {
    dblLatitude = Double.parseDouble(location.latitude);
    }
    catch (Exception e)
    {
    longitude.setText(e.toString());
    }
    ...

    This compiles, but when run the Exception is caught (I am using the longitude StringItem to show the error text) I see "java.lang.NullPointerException : 0" on the screen.

    I don't understand why, because location.latitude is of type String and contains seemingly valid data at this point, because I can see it on the screen as 5443.8845 in the StringItem I am using for latitude. I thought it might be whitespace at the end of the String, but I have used trim() and also shown the length() to be 9.

    Sadly, as this is a mobile app I can't really debug or run it on the PC as it uses bluetooth, so I deploy it each time I build it to test it out.

    Any helpful ideas as to why I can't convert a string containing 5443.8845 to a double ??

    It's doing my head in :confused: :eek:

    Cheers and thanks in advance,

    infaddict.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,459
    Rep Power
    25

    Default

    YOU need to post the full text of the error message together with the lines in your code where that is happening. Compile with the -g option to get source line numbers in the error text.
    In the code you show, what is the value of location? Can it be null?
    Add a println() to show its value before using it.

  3. #3
    infaddict is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi Norm, thanks for the prompt reply.

    I am already compiling with the -g option, but the error seems to report line zero (at least I am assuming thats what : "java.long.NullPointerException : 0" might mean).

    location could be null, but I believe it to be not null and contain valid information because I can show the information on the screen in the StringItems. If it was null, I believe I would not be able to show any of the information within it, which I can (when leaving everything in its native String format).

    I didn't post the FULL code here on my first post, because a) the classes are very long and one of them is written by someone else, so I didn't want to infringe any copyright... b) I thought it may be a common error and someone might spot it straight away from my code fragments.

    I've tried println() but that doesn't seem to work - nothing is shown on the phone screen. I believe this is because its a midlet - maybe println only works for full blown java apps or when ran on the PC via the console?

    Anyway, I think I may have taken a step closer to solving this. I think when the first couple of GPS data bursts are sent, they may contain invalid data. This causes the exception. Later on (after a few seconds), I think the data is clean, but my error message simply remains on the screen as I wasn't resetting that StringItem :rolleyes:.

    I am tinkering a little more now.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,459
    Rep Power
    25

    Default

    Can your app write to a permanent location, like a HD on a PC?
    If so, you get the output from println() by setting System.out to be to your code which would write to the location. I've done this by using a wrapper class that creates a PrintStream, uses setOut() and then starts the code to be tested. The println() method in my PrintStream captures the app's output and writes it where I can get it.

    Another idea, can you simulate the GPS output (capture a few minutes of its NMEA output) and use that as input to your program by extending the class that gets the GPS output and then have the program run on your PC? That would make first level debugging easier.

    What is a StringItem?

    I've written a bunch of GPS/waypoint related classes in Java that I used to edit/display routes and tracklogs from my GPS. I traveled by sailboat for several seasons and used a GPS for navigation. You're welcome to any/all of it. But its old code.

Similar Threads

  1. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM
  2. Converting URL to URI
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:15 AM
  3. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 PM
  4. Converting Epoch to string Date
    By amyedwards in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 10:33 PM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •