如何修复 NullPointerException,jsoup 元素

How to fix NullPointerException, jsoup element

我正在使用 Java Eclipse 和导入的 jsoup 包,以便从亚马逊网站上抓取。该程序只是转到搜索页面,查看结果数量,如果有任何更改,则会在屏幕左侧发出通知,并使用计时器不断重新加载页面。好吧,它运行良好,一段时间后,它不断向我显示错误。有时它会正确执行。

我尝试使用 if(select=!null),但它显​​示的结果为空。 这意味着 selector sometimes scrape data sometimes gets nulls。

    package main;

       import java.awt.AWTException;
       import java.awt.SystemTray;
       import java.io.IOException;
       import java.net.MalformedURLException;

       import org.jsoup.Jsoup;
       import org.jsoup.nodes.Document;
       import org.jsoup.nodes.Element;
       import org.jsoup.select.Elements;
       import java.util.Timer; 
       import java.util.TimerTask;
       class amazon1 extends TimerTask{
               public static int result1;
               public static int result2;
               public static int result3;
         public void run() {



        try {
            final Document doc = 
        Jsoup.connect("http://www.amazon.com/s?k=hello&i=stripbooks-intl- 
        ship&ref=nb_sb_noss")
                    .userAgent("Mozilla/17.0")
                    .get()

                    ;
      Elements select = doc.select("div.a-section.a-spacing- 
              small.a-spacing-top-small>span");
            Element first = select.first();
            String contentText = first.text();
            amazon1.result1 = 
             Integer.parseInt(contentText.replaceAll("[\D]",""));
                } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //



        if(amazon1.result1>amazon1.result2) {
        System.out.println(amazon1.result1);
        amazon1.result2 = amazon1.result1;
        amazon1.result3 = amazon1.result1 - amazon1.result2;
           if (SystemTray.isSupported()) {
                DisplayTrayIcon td = new DisplayTrayIcon();
                try {
                    td.displayTray();
                } catch (MalformedURLException | AWTException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                System.err.println("System tray not supported!");
            }

        } else {
         ;
        }






      }

   }



     public class Main {
       public static void main(String[] args) throws IOException {

         Timer timer = new Timer(); 
            TimerTask task = new amazon1(); 
            timer.schedule(task, 3000, 5000);



     }





   }

错误是..

    Exception in thread "Timer-0" java.lang.NullPointerException

  at main.amazon1.run(Main.java:31)

  at java.util.TimerThread.mainLoop(Timer.java:555)

  at java.util.TimerThread.run(Timer.java:505)

NullPointer 异常来自 selecting class 元素。 输出应该是 11620000 和屏幕右侧的通知。

Samuel Philipp 没有错误地暗示这个问题指的是 Whosebug 上的经典 "What is a NullPointerExcelption" 问题 (What is a NullPointerException, and how do I fix it?)

但是,由于您只是偶尔遇到 NullPointerException,因此值得考虑发生这种情况的原因。

我的猜测如下。有时,您尝试访问的网站不可用,或者它阻止了您的请求,例如,如果您过于频繁地重复访问该网站。在这种情况下,行

final Document doc = Jsoup.connect(
        "http://www.amazon.com/s?k=hello&i=stripbooksintl-ship&ref=nb_sb_noss")
    .userAgent("Mozilla/17.0")
    .get();

将以 doc == null 结束。这就是 select 方法随后因异常而失败的原因。

要解决此问题,您要么终止 catch (IOException e) 块,要么在 connect 方法后检查 doc 是否为 null。

} catch (IOException e) {
    // it seems the website is not reachable or the content is not according to the expectations.
    System.err.println("website not reachable or content malformed");
    // you may need to set the result1, result2, result3 variables accordingly here
    amazon1.rsult1 = 0;
    amazon1.rsult2 = 0;
    amazon1.rsult3 = 0;
    return;
}