gpt4 book ai didi

java - NullPointerException 解析 Jsoup

转载 作者:行者123 更新时间:2023-12-01 12:41:37 24 4
gpt4 key购买 nike

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

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“photos”是否存在来阻止它,但我仍然收到错误。

日志猫:

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

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

示例链接: http://www.forbes.com/colleges/cornell-university/没有照片 http://www.forbes.com/colleges/columbia-university-in-the-city-of-new-york/有照片吗

最佳答案

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

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

请注意我添加到您的 if 语句中的 photos.size() > 0select(..) statements in Jsoup will not return null if there are no elements. They will return an empty Elements list.您只需检查照片

的大小

关于java - NullPointerException 解析 Jsoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25046374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com