gpt4 book ai didi

java - getElementById 出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:53:41 24 4
gpt4 key购买 nike

我的代码在 content.html() 处出现空指针异常;每次将元素内容分配为 null 时虽然我确信该页面包含具有该 id 的元素我正在使用 jsoup 来解析文档请检查代码并发现我的错误

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

最佳答案

您正在打开文件并向其中写入空格字符,然后读取该文件进行解析。当您使用 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);
}
}

关于java - getElementById 出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24035191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com