不能对一个总是为空的简单Double []表影响值 [英] Can't affect values to a simple Double[] table which is always null

查看:186
本文介绍了不能对一个总是为空的简单Double []表影响值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含NMEA帧的文本文件。我检索$ GPGGA和$ GPRMC帧的纬度和经度。对于这部分,这是可以的。



现在,我想将纬度和经度转换为十进制度。当我尝试影响一个值到 Double [] coordinatestoconvert 时,会出现此问题。这一个总是空的。



就像这个错误真的是白痴,但是我今天早上转身就这么愚蠢...



<有人可以帮我吗?



这是我使用的方法:

 code> public String readText(String filepath)throws异常
{
String text =;
try
{
InputStream inputs = new FileInputStream(filepath);
InputStreamReader inputsreader = new InputStreamReader(inputs);

BufferedReader buffer = new BufferedReader(inputsreader);
字符串;
while((line = buffer.readLine())!= null)
{
/ *服务器发送给客户端全行。然后客户端将选择
*哪些数据将被检索* /

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

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);
}
返回文本;
}

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


if((splinedline [0] ==$ GPGGA)||(splitedline [0] ==$ GPRMC))
{
双[] coordinatestoconvert = NULL;
// coordinatetoconvert始终为null
coordinatestoconvert [0] = Double.parseDouble(splitedline [3]);
coordinatestoconvert [1] = Double.parseDouble(splitedline [5]);
coordinates = convertNmeaToDecimal(coordinatestoconvert);
}
返回坐标;
}

public Double [] convertNmeaToDecimal(Double [] coordinatestoconvert)
{
Double [] coordinatesconverted = null; (int i = 0; i <2; i ++)
{
Double degrees = coordinatestoconvert [i] / 100;
双倍时间= coordinatetoconvert [i] -degrees;

coordinatesconverted [i] = degrees + time / 60;
}
返回coordinateconverted;
}


解决方案

 双[] coordinatestoconvert = NULL; 

此行需要:

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

您还有与坐标转换相同的问题。



您还应该阅读标准的java样式和编码约定,因为它将使您的代码更容易阅读其他人。



您还在使用字符串比较==而不是.equals,这是无效的。



而且你可以获得更好的性能(如果这个程序很重要)使用double而不是Double。 / p>

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

解决方案

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.

这篇关于不能对一个总是为空的简单Double []表影响值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆