java - 如何修复 NullPointerException,jsoup 元素
问题描述
我正在使用 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 和来自屏幕右侧的通知。
解决方案
Samuel Philipp 没有错,暗示这个问题指的是 Stackoverflow 上的经典“什么是 NullPointerExcelption”问题(什么是 NullPointerException,我该如何解决?)
但是,由于您只是有时会遇到 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)
块,或者在方法之后检查 doc 是否为空connect
。
} 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;
}
推荐阅读
- eclipse - 在 Eclipse 编辑器中更改/删除悬停延迟
- java - JDBCTemplate 可选参数
- rest - MS Outlook 任务 REST API '% Complete' 字段
- c++ - std::strong_ordering 和 std::weak_ordering 的实际意义
- sql-server - SQL 中的板计算
- ffmpeg - 将 Chromium(或任何 Internet 浏览器)捕获到传输流中
- python - 反映 cx_Oracle / sqlalchemy 中的数据库表
- vba - Excel VBA;尝试将数据从工作簿中的一张工作表复制到同一工作簿中的另一张工作表时出现运行时错误“438”
- visual-studio-2017 - 每次构建本地解决方案时都可以在 Visual Studio 2017 中关闭自动运行测试吗?
- php - Api平台,route_name参数要输入什么?