HttpClient+Jsoup制作网络蜘蛛遇到问题,一直提示java.lang.NullPointerException

publicclassNetSpiderBiz{privatestaticCopyOnWriteArrayList<String>seeds=null;privatest... public class NetSpiderBiz {
private static CopyOnWriteArrayList<String> seeds = null;
private static CopyOnWriteArrayList<String> visited = null;
static {
seeds = new CopyOnWriteArrayList<String>();
visited = new CopyOnWriteArrayList<String>();
}
private CloseableHttpClient httpClient = null;
private HttpGet httpGet = null;
private HttpEntity entity = null;
private HttpResponse response = null;
private InputStream inputStream = null;
private InputStreamReader inputStreamReader = null;
private BufferedReader reader = null;
private Document document = null;
private Elements elements = null;
public void run() {
try {
if (seeds != null && seeds.size() > 0 && !seeds.isEmpty()) {
for (String seed : seeds) {
if (seed != null && seed.trim().length() > 0 && !"".equals(seed)) {
if (!visited.contains(seed)) {
httpClient = HttpClientBuilder.create().build();
httpGet = new HttpGet(seed);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
entity = response.getEntity();
inputStream = entity.getContent();
inputStreamReader = new InputStreamReader(inputStream , "utf-8");
reader = new BufferedReader(inputStreamReader);
String line = null;
String code = "";
while ((line = reader.readLine()) != null) {
code += line;
}
System.out.println(code);

visited.add(seed);
seeds.remove(seed);

document = Jsoup.parse(code);
if (document != null) {

}
elements = document.body().select("a");
/*if (elements != null && elements.size() > 0) {
for (Element e : elements) {
String a = e.attr("abs:href");
if (a != null && a.trim().length() > 0 && !"".equals(a)) {
if (!seeds.contains(a)) {
seeds.add(a);
System.out.println(e.text() + " : " + e.attr("abs:href"));
}
}
}
}*/
}
}
}
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}/* finally {
try {
reader.close();
inputStreamReader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
//run();
}

public static void main(String[] args) {
NetSpiderBiz n = new NetSpiderBiz();
seeds.add("http://www.sste.com/its/default.htm");
n.run();
}

}
展开
 我来答
l7722526
2019-09-09 · TA获得超过2.7万个赞
知道大有可为答主
回答量:2.8万
采纳率:84%
帮助的人:3796万
展开全部
你的 getAllId() 这个方法执行 for (String string : t.getNextUrl(maxSize)) 这个循环的时候报错了.
因为你的 getNextUrl() 方法最终返回的是 null 所以这里遍历肯定不行
Forever小熊
2019-09-09 · 超过42用户采纳过TA的回答
知道小有建树答主
回答量:204
采纳率:57%
帮助的人:54.8万
展开全部
设置断点在debug里面找,话说你属性为啥设置那么多null
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式