java.lang.NullPointerException: null
at com.gargoylesoftware.htmlunit.html.XmlSerializer.asXml(XmlSerializer.java:73)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.save(XmlSerializer.java:61)
at com.gargoylesoftware.htmlunit.html.HtmlPage.save(HtmlPage.java:2176)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.getAttributesFor(XmlSerializer.java:174)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.readAttributes(XmlSerializer.java:140)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printOpeningTag(XmlSerializer.java:120)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:90)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.asXml(XmlSerializer.java:80)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.save(XmlSerializer.java:61)
at com.gargoylesoftware.htmlunit.html.HtmlPage.save(HtmlPage.java:2176)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.getAttributesFor(XmlSerializer.java:174)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.readAttributes(XmlSerializer.java:140)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printOpeningTag(XmlSerializer.java:120)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:90)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.printXml(XmlSerializer.java:100)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.asXml(XmlSerializer.java:80)
at com.gargoylesoftware.htmlunit.html.XmlSerializer.save(XmlSerializer.java:61)
at com.gargoylesoftware.htmlunit.html.HtmlPage.save(HtmlPage.java:2176)
at info.fmro.betty.utility.WebScraperMethods.savePage(WebScraperMethods.java:70)
at info.fmro.betty.utility.WebScraperMethods.savePage(WebScraperMethods.java:58)
at info.fmro.betty.utility.WebScraperMethods.getPage(WebScraperMethods.java:191)
I was simply trying to save a HtmlPage containing javascript code. As the javascript was running, something was probably updated in the page that caused the null pointer exception.
This is the line of code where the exception occured(line 73 of com.gargoylesoftware.htmlunit.html.XmlSerializer):
final SgmlPage page = node.getPage();
I would guess the variable "node" was null.
This is not easy to reproduce, as it seems to happen quite rarely and it depends on the way the page is updated by the running javascript.
In this case,
clone/cloneNode()the page before serializing.I've already managed the exception in my program.
Using clone() on the page might add unnecessary overhead for a fairly rare exception. It's much cheaper to just try again (maybe with clone() the second time).
But adding a check for null in the HtmlUnit code would be a more permanent solution.
I see now that HtmlPage.clone() can't be used and I don't know the effects of cloneNode().
But as I said, in my program the problem is simply fixed by trying again.