日期:2014-05-20  浏览次数:20741 次

实在不明白为什么运行不了,求解答
package com.cl.wx.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class WeatherTest {

public static void main(String[] args) {
InputStream is = null;
try {
URL u = new URL(
"http://php.weather.sina.com.cn/xml.php?city=上海&password=DJOYnieT8234jlsK&day=1");
HttpURLConnection uConnection = (HttpURLConnection) u
.openConnection();
uConnection.connect();
is = uConnection.getInputStream();
SAXReader reader = new SAXReader();
Document document = reader.read(is);
Element root = document.getRootElement();
Element weather = root.element("Weather");
List<Element> elementList = weather.elements();
Map<String, String> map = new HashMap<String, String>();
for (Element e : elementList)
map.put(e.getName(), e.getText());

System.out.println(map.get("city"));
System.out.println(map.get("gm_s"));

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}

}

}


用的dom4j读取XML,我发给同学,在他那里就可以运行 ,在我电脑上就运行不了
错误如下:
Exception in thread "main" java.lang.NullPointerException
at com.cl.wx.test.WeatherTest.main(WeatherTest.java:32)
------解决方案--------------------
引用:
Quote: 引用:

weather为空,打断点跟一下
我今天又发现上面的地址返回的就是空。。。。
解决了?