无法理解 Java 异常

如何解决无法理解 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?