1

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;
                }
            }
        }
    }
5
  • You should make sure that doc1 and doc2 are different documents. Commented Apr 8, 2013 at 13:16
  • Yes both are different. Commented Apr 8, 2013 at 13:18
  • @Alohci Can I discuss on this. I will explain my code on chat? Commented Apr 8, 2013 at 13:23
  • 1
    Sorry, no. Your code looks to have problems, but since I don't know JSoup, I'm not sure which one(s) apply. I suggest you change the code so that you're not removing elements in the inner loop like that. Build a list of elements that you want to remove, then loop backwards once through the elements list for each dom to remove the elements. Commented Apr 8, 2013 at 13:56
  • @Alohci Ohk, I will do that. But when I remove elements from both lists, then I put break. It does the same, that you suggests. Commented Apr 8, 2013 at 14:22

2 Answers 2

3

The error happens when one tries to remove an element whose parent has already been removed. This could point to a bug in Jsoup. Doesn't removing a parent also remove the children?

A workaround is to first check if parent exists:

if (element != null && element.parent() != null) { // fixes java.lang.IllegalArgumentException in org.jsoup.helper.Validate.notNull
    element.remove();
}
Sign up to request clarification or add additional context in comments.

1 Comment

It might happen if you are iterating elements and delete the parent before its children so probably jsoup is behaving correctly but the error is modifying the elements in a loop
2

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

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

8 Comments

But this problem not arises regulary. Sometimes it arises, sometimes it runs fine.
Surely elem1 and elem2 must not be null for the .id() accessor to not throw in the if(...)?
@Adrian Yes I checked this at the place where I commented // Check if elem1 exists in DOM, If No, then continue, If that element is not in DOM then continue, otherwise proceed.
Is this something you can debug? Put a break point on elem1.remove(), and check the objects involved.... ( I'm no Java expert, and I cant discern what platform, environment you are running this code on)
@Adrian I am making my project on maven, coding in java. USing Jsoup library for making DOM.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.