如何修复NullPointerException,jsoup元素

时间:2019-05-20 18:55:49

标签: java timer jsoup amazon screen-scraping

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

我尝试使用if(select =!null),但显示为空结果。 这意味着选择器有时会抓取数据,有时会得到空值。

    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异常来自选择类元素。 输出应为11620000,并在屏幕右侧显示通知。

1 个答案:

答案 0 :(得分:0)

塞缪尔·菲利普(Samuel Philipp)没错,这表明该问题涉及Stackoverflow(What is a NullPointerException, and how do I fix it?)上的经典“什么是NullPointerExcelption”问题

但是,由于您仅有时会遇到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方法失败并显示Exception的原因。

要解决此问题,您可以终止catch (IOException e)块,或者在connect方法之后检查doc是否为空。

} 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;
}