我的代码在content.html()中获得空指针异常; 因为每次元素内容被赋值为null 虽然我确信该页面包含具有该ID的元素 我正在使用 jsoup 来解析Document 我可以检查代码并发现我的错误
public void get_content(String eliment_by,String identification)
{
try {
File currentDirectory = new File(new File(".").getAbsolutePath());
System.out.println(currentDirectory.getCanonicalPath());
PrintWriter writer = new PrintWriter(currentDirectory.getCanonicalPath()+"/tmp/input.html", "UTF-8");
writer.println(" ");
writer.close();
File input = new File(currentDirectory.getCanonicalPath()+"/tmp/input.html");
org.jsoup.nodes.Document doc = Jsoup.parse(input, "UTF-8", this.curr_url);
Element content = doc.getElementById(identification);
this.current_page_content=content.html();
} catch (IOException ex) {
Logger.getLogger(url_looping.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:1)
您正在打开文件并为其写入空格字符,然后读取此文件以进行解析。当您使用PrintWriter
时,这会清除您的文件。那么这里发生的是首先清除文件,然后尝试解析它。这就是空指针
如果你想从网址获取,你可以这样做
public void get_content(String eliment_by, String identification) {
try {
org.jsoup.nodes.Document doc = Jsoup.connect("http://www.opengurukul.com/vlc/mod/page/view.php?id=523").get();
Element content = doc.getElementById(identification);
this.current_page_content = content.html();
} catch (IOException ex) {
Logger.getLogger(url_looping.class.getName()).log(Level.SEVERE,
null, ex);
}
}