Can't affect values to a simple Double[] table which is always null

女生的网名这么多〃 提交于 2019-12-25 03:10:41

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/20608871/cant-affect-values-to-a-simple-double-table-which-is-always-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!