如何解决无法理解 Java 异常
我正在使用 java 编写搜索引擎代码,但我在不知道原因的情况下收到此错误:
Exception in thread "main" java.lang.NullPointerException
at WritetoFile.fileWriter(WritetoFile.java:29)
at Main.main(Main.java:14)
Process finished with exit code 1
这是我的代码:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class Search {
private static String URL="https://www.google.com/search?q=";
private Document doc;
private Elements links;
private String html;
public Search() throws IOException {};
public void SearchWeb() throws IOException {
//to get the keywords from the user
Scanner sc = new Scanner(system.in);
System.out.println("Please enter the keyword you want to search for: ");
String word = sc.nextLine();
//Search for the keyword over the net
String url = URL + word;
doc = Jsoup.connect(url).get();
html = doc.html();
Files.write(Paths.get("D:\\OOP\\OOPproj\\data.txt"),html.getBytes());
links = doc.select("cite");
}
public Document getDoc() {
return doc;
}
public String getHtml() {
return html;
}
public Elements getLinks() {
return links;
}
}
这是 writetoFile 类:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenoption;
import java.util.ArrayList;
public class WritetoFile extends Search {
public WritetoFile() throws IOException {};
String description = "<!> Could not fetch description <!>";
String keywords = "<!> Could not fetch keywords <!>";
private ArrayList<String> detail = new ArrayList<String>();
BufferedWriter bw = null;
public void fileWriter() throws IOException {
for (Element link : super.getLinks()) {
String text = link.text();
if (text.contains("›")) {
text = text.replaceAll(" › ","/");
}
detail.add(text);
System.out.println(text);
}
System.out.println("***************************************************");
for (int i = 0; i < detail.size(); i++)
System.out.println("detail [" + (i + 1) + "]" + detail.get(i));
System.out.println("###################################################################");
for (int j = 0; j < detail.size(); j++) {
Document document = Jsoup.connect(detail.get(j)).get();
String web = document.html();
Document d = Jsoup.parse(web);
Elements MetaTags = d.getElementsByTag("Meta");
for (Element MetaTag : MetaTags) {
String content = MetaTag.attr("content");
String name = MetaTag.attr("name");
if ("description".equals(name)) {
description = content;
}
if ("keywords".equals(name)) {
keywords = content;
}
}
String title = d.title();
Files.write(Paths.get("D:\\OOP\\OOPproj\\search.txt"),(detail.get(j) + "\t" + "|" + "\t" + title + "\t" + "|" + "\t" + description + "\t" + "|" + "\t" + keywords + System.lineseparator()).getBytes(),StandardOpenoption.APPEND);
}
}
}
这是主类:
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Search a = new Search();
a.SearchWeb();
WritetoFile b = new WritetoFile();
b.fileWriter();
}
}
我试图在 main 中打印 getLinks() 方法以检查它是否为 null ,但它不是,链接被引用。 如果有人帮助我,我将不胜感激。
解决方法
您在对象 Scenario Outline:
Given url 'http://dev.companyname.com'
When path "/some/path/"+id
Then method Get
Then status 200
And def id = response.id
Examples:
|read('classpath:com/dev_testdata.csv')|
上调用 SearchWeb()
,但您在对象 a
上调用 fileWriter()
。这意味着链接在 b
中设置,但不在 a
中。
由于 b
扩展了 WriteToFile
,您只需要它的一个实例:
Search
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。