正如在很多論壇上所說的那樣,元素是DOM中節點的特例。爲什麼給代碼拋出異常(Jsoup)
但是我得到了違反這個規則的異常。
它在語句elem.remove()
處引發異常。
這裏,ele是一個元素。 remove()
是Jsoup API中的一個函數,它從DOM中刪除節點及其後代。
例外: -
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalArgumentException: Object must not be null
at org.jsoup.helper.Validate.notNull(Validate.java:16)
at org.jsoup.nodes.Node.remove(Node.java:266)
at XXX.YYY.ZZZ.Template_Matching.Template_Matching.removeProductLister(Template_Matching.java:80)
at XXX.YYY.ZZZ.Template_Matching.Template_Matching.main(Template_Matching.java:376)
... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
代碼: -
public static void function(Document doc1, Document doc2, String tag) {
//Checking for ULs
Elements uls_1 = doc1.getElementsByTag(tag);
Elements uls_2 = doc2.getElementsByTag(tag);
for (Element elem1 : uls_1) {
// Check if elem1 exists in DOM, If No, then continue
for (Element elem2 : uls_2) {
// Check if elem2 exists in DOM, If No, then continue
// If id matches, remove them
if ((!"".equals(elem1.id())) && (elem1.id().equals(elem2.id()))) {
elem1.remove();
elem2.remove();
break;
}
}
}
}
你應該確保DOC1和DOC2是不同的文件。 – Alohci 2013-04-08 13:16:57
是的,兩者是不同的。 – devsda 2013-04-08 13:18:24
@Alohci我可以就此進行討論。我將在聊天時解釋我的代碼? – devsda 2013-04-08 13:23:02