我一直收到此错误
NullPointer
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): Caused by: java.lang.NullPointerException
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): at com.fttech.htmlParser.releaseInfo.onCreate(releaseInfo.java:62)
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
它指向我的元素
Element paragraph = overview.select("p").last();
我正在使用它来检索文章
try {
doc = Jsoup.connect(url).get();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(doc == null){
Toast.makeText(this, "Couldnt retrieve game info", Toast.LENGTH_SHORT);
}
else{
// Get the overview div
Element overview = doc.select("div#object-overview").last();
答案 0 :(得分:1)
每当你在一个链中找到一个带select(“”)的元素你的调用last(),假设它总是找到至少1个元素,在没有说“p”的情况下“在文件中,就是当你遇到崩溃时。
这只是简单的NullPointerExceptions,你需要学习防御性编码:
// If you believe overview could be null
if(overview != null){
ArrayList<Element> paragraphs = overview.select("p"); // Whatever type select(String) returns
Element lastParagraph = null;
if(paragraphs != null){
lastParagraph = paragraphs.last();
} else {
// Deal with not finding "p" (lastParagraph is null in this situation)
}
// Continue with lastParagraph
} else {
// Deal with overview being null
}
Number 1 Java Error(向下滚动)
此外,您不应该使用catch all Exception包装代码,尝试捕获每个异常并单独处理它们。
查找get()方法Jsoup get()的API(无论如何eclipse告诉你)它会抛出IOException,所以你应该只是抓住它。
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
Log.e("Tag", "Jsoup get didn't get a document", e);
}
Number 5 Java Error(向下滚动)