Question

I have a text file containing NMEA frames. I retrieve the latitude and the longitude of $GPGGA and $GPRMC frames. For this part, it was okay.

Now, I'd like to convert the latitude and longitude into Decimal Degrees. The problem occur when I try to affect a values to Double[]coordinatestoconvert. This one is always null.

It's like this error is really idiot, but I turn around all this morning for such a foolishness...

Can someone help me please ?

Here are the methods I am using :

public String readText(String filepath) throws Exception
{
    String text="";
    try 
    {
        InputStream inputs=new FileInputStream(filepath);
        InputStreamReader inputsreader=new InputStreamReader(inputs);

        BufferedReader buffer=new BufferedReader(inputsreader);
        String line;
        while((line=buffer.readLine())!=null)
        {
            /* Server send to Client the full line. Then Client will select
             * which data will be retrieve */

            String[]splitedline=line.split(",");
            Double[]decimalcoordinates=retrieveCoordinates(splitedline);

            messagearea.append(decimalcoordinates[0].toString()+","+decimalcoordinates[1].toString());
            tcpserver.sendMessage(decimalcoordinates[0].toString()+","+decimalcoordinates[1].toString());

        }
        buffer.close();
    } 
    catch(FileNotFoundException e) 
    {
        System.out.println(e);
    }   
    return text;
}

public Double[] retrieveCoordinates(String[] splitedline)
{
    Double[]coordinates=null;


    if((splitedline[0]=="$GPGGA") || (splitedline[0]=="$GPRMC"))
    {
        Double[]coordinatestoconvert=null;
        // coordinatestoconvert is always null here
        coordinatestoconvert[0]=Double.parseDouble(splitedline[3]);
        coordinatestoconvert[1]=Double.parseDouble(splitedline[5]);
        coordinates=convertNmeaToDecimal(coordinatestoconvert);
    }
    return coordinates;
}

public Double[] convertNmeaToDecimal(Double[] coordinatestoconvert)
{
    Double[]coordinatesconverted=null;
    for(int i=0;i<2;i++)
    {
        Double degrees=coordinatestoconvert[i]/100;
        Double time=coordinatestoconvert[i]-degrees;

        coordinatesconverted[i]=degrees+time/60;
    }
    return coordinatesconverted;
}
Was it helpful?

Solution

Double[]coordinatestoconvert=null;

This line needs to be:

Double[] coordinatestoconvert=new Double[coordinatestoconvert.length];

You also have the same problem with coordinatesconverted.

You should also read up on the standard java style and coding conventions as it will make your code easier to read for others.

You are also doing string comparison using == rather than .equals, that is invalid.

And you will get better performance (if that matters for this program) using double rather than Double wherever you can.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top