我在我的android应用程序中有一个jsoup代码,用于检查网站上是否有div,但它使我返回“porukeDiv IS NOT NULL”,即使它是。下面的代码:为什么Jsoup div如果为空则不会给出“null”?
Document doc = Jsoup.parse(html);
// Now you can, for example, retrieve a div with id="username" here
Elements porukeDiv = doc.select("div#logovan > a[header-pvtmsg-link]"); //bilo Element
String porukeText = porukeDiv.text();
System.out.println("porukeDiv: " + porukeDiv);
System.out.println("porukeText: " + porukeText);
if (porukeDiv != null) {
System.out.println("porukeDiv NOT NULL! ");
if (porukeText.startsWith("Poruke (")) {
loggedin[0] = true;
startstopAlarmPoruke();
}
}else{
loggedin[0] = false;
startstopAlarmPoruke();
System.out.println("porukeDiv NULL! ");
但是,当它为空,这是信息,我得到的logcat:
11-21 06:14:25.092 868-927/com.package.pack I/System.out﹕ porukeDiv:
11-21 06:14:25.092 868-927/com.package.pack I/System.out﹕ porukeText:
11-21 06:14:25.092 868-927/com.package.pack I/System.out﹕ porukeDiv NOT NULL!
为什么出现这种情况?
这definitelly不为空。如果是这样,'String porukeText = porukeDiv.text();'会抛出NPE。 – 2014-11-21 11:23:28