I wanted to try out HtmlUnit's new JavaScript and AJAX support. I downloaded the latest version (2.2) and used the following code for a simple test:
============================================
import com.gargoylesoftware.htmlunit.;
import com.gargoylesoftware.htmlunit.html.;
import org.apache.commons.codec.*;
public class NewHtmlUnitTest {
public NewHtmlUnitTest() { } public static void main(String [] args) { try { System.out.println("Initializing web client..."); final WebClient webClient = new WebClient(); System.out.println("Loading www.amazon.com..."); final HtmlPage page = (HtmlPage) webClient.getPage("http://www.amazon.com/"); System.out.println(page.asText()); } catch (Exception e) { System.out.println("Drat: " + e.toString()); e.printStackTrace(); } }
When I run this code, I receive the following error:
============================================
Initializing web client...
Loading www.amazon.com...
Jul 24, 2008 3:47:01 PM com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument jsxFunction_execCommand
WARNING: Nothing done for execCommand(BackgroundImageCache, ...) (feature not implemented)
Jul 24, 2008 3:47:02 PM com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument jsxFunction_execCommand
WARNING: Nothing done for execCommand(BackgroundImageCache, ...) (feature not implemented)
Jul 24, 2008 3:47:02 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:02 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:02 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:02 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:02 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:02 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:03 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:03 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Jul 24, 2008 3:47:03 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter warning
WARNING: warning: message=[Calling eval() with anything other than a primitive string value will simply return the value. Is this what you intended?] sourceName=[http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/n2CoreLibs/n2CoreLibs-events-9331.V6303603.js] line=[564] lineSource=[null] lineOffset=[0]
Drat: com.gargoylesoftware.htmlunit.ScriptException: Wrapped java.lang.NullPointerException (script in http://www.amazon.com/ from (1665, 57) to (1669, 10)#1667)
======= EXCEPTION START ========
Exception class=[org.mozilla.javascript.WrappedException]
com.gargoylesoftware.htmlunit.ScriptException: Wrapped java.lang.NullPointerException (script in http://www.amazon.com/ from (1665, 57) to (1669, 10)#1667)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:528)
at org.mozilla.javascript.Context.call(Context.java:502)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:405)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:380)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:889)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:258)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:311)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:206)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:498)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:468)
at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:210)
at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:992)
at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:895)
at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:2835)
at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:1940)
at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:880)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:495)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:448)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:636)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:237)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:127)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:101)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:440)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:388)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:375)
at NewHtmlUnitTest.main(NewHtmlUnitTest.java:16)
Caused by: org.mozilla.javascript.WrappedException: Wrapped java.lang.NullPointerException (script in http://www.amazon.com/ from (1665, 57) to (1669, 10)#1667)
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1657)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:176)
at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:415)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3335)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2484)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:169)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3004)
at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$5.doRun(JavaScriptEngine.java:396)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:522)
... 30 more
Caused by: java.lang.NullPointerException
at com.gargoylesoftware.htmlunit.html.HtmlPage.writeInParsedStream(HtmlPage.java:2095)
at com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument.write(HTMLDocument.java:319)
at com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument.jsxFunction_write(HTMLDocument.java:234)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
... 40 more
WrappedException: org.mozilla.javascript.WrappedException: Wrapped java.lang.NullPointerException (script in http://www.amazon.com/ from (1665, 57) to (1669, 10)#1667)
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1657)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:176)
at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:415)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3335)
at script(script in http://www.amazon.com/ from (1665, 57) to (1669, 10):1667)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2484)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:169)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3004)
at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$5.doRun(JavaScriptEngine.java:396)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:522)
at org.mozilla.javascript.Context.call(Context.java:502)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:405)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:380)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:889)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:258)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:311)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:206)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:498)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:468)
at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:210)
at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:992)
at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:895)
at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:2835)
at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:1940)
at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:880)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:495)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:448)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:636)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:237)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:127)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:101)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:440)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:388)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:375)
at NewHtmlUnitTest.main(NewHtmlUnitTest.java:16)
Caused by: java.lang.NullPointerException
at com.gargoylesoftware.htmlunit.html.HtmlPage.writeInParsedStream(HtmlPage.java:2095)
at com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument.write(HTMLDocument.java:319)
at com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument.jsxFunction_write(HTMLDocument.java:234)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
... 40 more
Inside wrapped exception:
java.lang.NullPointerException
at com.gargoylesoftware.htmlunit.html.HtmlPage.writeInParsedStream(HtmlPage.java:2095)
at com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument.write(HTMLDocument.java:319)
at com.gargoylesoftware.htmlunit.javascript.host.HTMLDocument.jsxFunction_write(HTMLDocument.java:234)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:415)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3335)
at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2484)
at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:169)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3004)
at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$5.doRun(JavaScriptEngine.java:396)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:522)
at org.mozilla.javascript.Context.call(Context.java:502)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:405)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:380)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:889)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:258)
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:311)
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:206)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:498)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:468)
at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:210)
at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:992)
at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:895)
at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:2835)
at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:1940)
at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:880)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:495)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:448)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:636)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:237)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:127)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:101)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:440)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:388)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:375)
at NewHtmlUnitTest.main(NewHtmlUnitTest.java:16)
======= EXCEPTION END ========
============================================
Is this expected behavior?
Logged In: YES
user_id=402164
Originator: NO
A NPE is never the expected behavior ;-(
The problem seems to be caused by a document.write call that occurs once the document has been totally parsed. If you can investigate to reproduce the problem with a small code example, this would be helpful.
Logged In: YES
user_id=2059149
Originator: YES
I will try and isolate the code snippet that is causing this as well as where the NPE is coming from.
Logged In: YES
user_id=2059149
Originator: YES
Here is what I have done thus far.
(1) I called setJavaScriptEnabled(false) to see if the NPE was still thrown. It was not. So, clearly this is related to some JavaScript on the page.
(2) I looked at the snippet of JavaScript that was causing the NPE to be thrown. It was throwing the NPE when loading a section of code like the following:
However, if I put just that into an HTML file, HtmlUnit works fine, which leads me to believe that HtmlUnit is running out of memory. If I comment out the Javascript snippet that it is failing on, HtmlUnit continues on to the next snippet and errors out.
Any ideas?
Logged In: YES
user_id=402164
Originator: NO
I don't get any error with latest sources from SVN. Perhaps the problem has been fixed, perhaps the amazon page has changed.
Logged In: YES
user_id=1985008
Originator: NO
NPE occurring in HtmlPage.writeInParsedStream() has been fixed. There was specific bug report #2052115 for the same.