【问题标题】:Java Exception not understoodJava 异常不理解
【发布时间】:2021-08-09 02:45:52
【问题描述】:

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

【问题讨论】:

  • 第 29 行是哪一行?快速浏览一下,linklink.text() 中可能为空,或者texttext.contains(",") 中可能为空,如果错误来自那里。
  • 这是第 29 行 for (Element link : super.getLinks())
  • 那么getLinks() 必须返回null。尝试在 fileWriter() 的 for 循环之前打印 getLinks() 的结果以确认这一点。
  • 你是对的,它确实返回 null。但为什么?当我在主页面打印它时,它给了我链接
  • 首先想到的是链接是通过SearchWeb()方法初始化的,所以必须在fileWriter()之前调用。

标签: java class oop search-engine


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多