Java 不理解异常

Java Exception not understood

我正在使用 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;
   }
}


这是 class 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);
            }

        }
}

这是主要的 class:

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 ,但它不是,链接被引用了。 如果有人能帮助我,我将不胜感激。

您在对象 a 上调用 SearchWeb(),但在对象 b 上调用 fileWriter()。这意味着链接设置在 a 中,而不是在 b.

由于 WriteToFile 扩展了 Search,您只需要一个实例:

WriteToFile a = new WriteToFile();
a.SearchWeb();
a.fileWriter();