私はJavaでコードを書いていて、このURLからxmlを解析しようとしています :
この URL は google API に属しており、2 つのポイント (src、dest) を受け取り、それらの間のルートを xml で返します。
プログラムをEclipseでデバッグすると、完璧に動作します。しかし、デバッグなしでコードを実行すると、エラーが返されます。(関数の最後にブレークポイントを置くと、「dist」がnullになり、理由がわかりません)なぜそれが起こるのですか?
コードは
public double calcDist(Point p) //p=the src of the ride (the dest in the calculation)
{
String src = Double.toString(this.lati);
src = src.concat(",");
src = src.concat(Double.toString(this.longi));
String dest = Double.toString(p.lati);
dest = dest.concat(",");
dest = dest.concat(Double.toString(p.longi));
String dist=null;
URL url = null;
try
{
url = new URL("https://maps.googleapis.com/maps/api/directions/xml?origin="+src+"&destination="+dest+"&sensor=false");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
Document doc = null;
doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("leg");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
NodeList nodeList2 =node.getChildNodes();
for (int j = 0; j < nodeList2.getLength(); j++)
{
Node child = nodeList2.item(j);
if (child.getNodeName().contentEquals("distance"))
{
NodeList nodeList3 = child.getChildNodes();
for (int p1 = 0; p1 < nodeList3.getLength(); p1++)
{
Node child2 = nodeList3.item(p1);
if (child2.getNodeName().contentEquals("text"))
{
Node tmp = child2.getFirstChild();
if (tmp != null)
dist = child2.getFirstChild().getNodeValue();
}
}
}
}
}
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
dist = dist.substring(0, dist.length()-3);
return Double.parseDouble(dist);
}