为什么给代码抛出异常(Jsoup) [英] Why given code throws exception (Jsoup)

查看:128
本文介绍了为什么给代码抛出异常(Jsoup)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在这么多论坛上所说的那样,元素是DOM中的一个特殊情况。



但是我有一个例外,违反了这个规则。 >

它在语句 elem.remove()中抛出异常。



这里,ele是一个元素。 remove()是Jsoup API中的一个功能,可以从DOM中删除节点及其后代。



例外: -

  [警告] 
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)
在java.lang.reflect.Method.invoke(Method.java:601)
在org.codehaus.mojo.exec.ExecJavaMojo $ 1.run(ExecJavaMojo.java:297)
在java .lang.Thread.run(Thread.java:722)
导致:java.lang.IllegalArgumentException:对象不能为null
在org.jsoup.helper.Validate.notNull(Validate.java: 16)
在org.jsoup.nodes.Node.remove(Node.java:266)
在XXX.YYY.ZZZ.Template_Matching.Template_Matching.removeProductLister(Template_Matching.java:80)
在XX X.YYY.ZZZ.Template_Matching.Template_Matching.main(Template_Matching.java:376)
... 6更多
[INFO] ---------------- -------------------------------------------------- ------
[INFO] BUILD FAILURE

strong>: -

  public static void function(Document doc1,Document doc2,String tag){

//检查ULs
元素uls_1 = doc1.getElementsByTag(tag);
元素uls_2 = doc2.getElementsByTag(tag); (元素elem1:uls_1){

//检查DOM中是否存在elem1,如果否,则继续

(Element elem2 :uls_2){

//检查DOM中是否存在elem2,如果否,则继续

//如果id匹配,删除它们
if((! .equals(elem1.id()))&(elem1.id()。equals(elem2.id()))){
elem1.remove();
elem2.remove();
break;
}
}
}
}


解决方案

导致:java.lang.IllegalArgumentException:对象不能为空



您需要检查您是否访问空对象...


As it is said on so many forums that element is a special case of node in DOM.

But I got an exception that violate this rule.

It throws exception at statement, elem.remove().

Here, ele is an element. remove() is a function in Jsoup API, that removes nodes and their descendants from DOM.

Exception :-

[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

Code :-

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

解决方案

Caused by: java.lang.IllegalArgumentException: Object must not be null

You need to check that you aren't accessing properties of null objects...

这篇关于为什么给代码抛出异常(Jsoup)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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