NullPointerException 解析 Jsoup [英] NullPointerException Parsing Jsoup

查看:37
本文介绍了NullPointerException 解析 Jsoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Jsoup 解析网站时收到 NullPointerException.解析的实际网页根据用户输入而变化.本质上,有时网页上有图像,有时没有.

I am getting a NullPointerException while using Jsoup to parse a website. The actual webpage which is parsed changes depending on user input. Essentially, sometimes there are images on the webpage and sometimes there are not.

Document doc = Jsoup.connect("http://www.forbes.com/colleges/"+college+"/").get();
Elements photos = doc.select("div[id=photos]");
if(photos!=null){                   
    imgSrc1 = photos.select("li").select("img").first().attr("src");
    input1 = new java.net.URL(imgSrc1).openStream();
    bitmap1 = BitmapFactory.decodeStream(input1);
    image1.setImageBitmap(bitmap1);

}else{
    buffer.append("No Pictures");
}

截至目前,对于有图像的网页,它们都能正常显示.对于那些没有图像的人,我会抛出 NPE.我尝试通过检查 ID照片"是否存在来阻止它,但我仍然收到错误消息.

As of now, for the webpages which do have images, they show up properly. For those that do not have images, I get a NPE thrown. I tried preventing it by checking whether the id "photos" is present or not, but I am still getting the error.

Logcat:

07-30 16:24:43.497: E/AndroidRuntime(30847): FATAL EXCEPTION: main
07-30 16:24:43.497: E/AndroidRuntime(30847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.collegeselector/com.collegeselector.CollegeInfo}: java.lang.NullPointerException
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.os.Looper.loop(Looper.java:137)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.ActivityThread.main(ActivityThread.java:5039)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at java.lang.reflect.Method.invoke(Method.java:511)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at dalvik.system.NativeStart.main(Native Method)
07-30 16:24:43.497: E/AndroidRuntime(30847): Caused by: java.lang.NullPointerException
07-30 16:24:43.497: E/AndroidRuntime(30847):    at com.collegeselector.CollegeInfo.onCreate(CollegeInfo.java:92)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.Activity.performCreate(Activity.java:5104)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-30 16:24:43.497: E/AndroidRuntime(30847):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-30 16:24:43.497: E/AndroidRuntime(30847):    ... 11 more

我应该怎么做才能解决这个问题?

What should I do to fix this?

示例链接:http://www.forbes.com/colleges/cornell-university/ 没有有照片http://www.forbes.com/Colleges/columbia-university-in-the-纽约市/确实有照片

Sample links: http://www.forbes.com/colleges/cornell-university/ does NOT have the photos http://www.forbes.com/colleges/columbia-university-in-the-city-of-new-york/ does have the photos

推荐答案

您需要检查您的照片元素是否确实包含项目

You need to check if your photos element actually contains items

Document doc = Jsoup.connect("http://www.forbes.com/colleges/"+college+"/").get();
Elements photos = doc.select("div[id=photos]");
if(photos!=null && photos.size() > 0){                   
    imgSrc1 = photos.select("li").select("img").first().attr("src");
    input1 = new java.net.URL(imgSrc1).openStream();
    bitmap1 = BitmapFactory.decodeStream(input1);
    image1.setImageBitmap(bitmap1);
}else{
    buffer.append("No Pictures");
}

注意 photos.size() >0 我添加到你的 if 语句中.select(..)Jsoup 中的 code> 语句不会返回 null.他们将返回一个空的 Elements 列表. 你只需要检查 photos

Notice the photos.size() > 0 I added to your if statement. select(..) statements in Jsoup will not return null if there are no elements. They will return an empty Elements list. You just need to check the size of photos

这篇关于NullPointerException 解析 Jsoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆