Gehad Gamal
Gehad Gamal

Reputation: 101

Htmlunit null pointer on filling input , but element is not nulled

So i am trying to automate a login through htmlunit and here are my trials so far , however the output always have null pointer exception , although the element is not nulled

 public static void main(String[] args) throws IOException, InterruptedException {
    final WebClient webClient = new WebClient(BrowserVersion.CHROME);
    final HtmlPage page = webClient.getPage("https://sellercentral.amazon.com");
    final HtmlForm form = page.getFormByName("signIn");
    final HtmlTextInput un = form.getInputByName("email");
    final HtmlPasswordInput pass = form.getInputByName("password");
    System.out.println(un);
    System.out.println(pass);
    /*Trial 1*/
    un.select();
    un.type("[email protected]");
    System.out.println(un);

    /*Trial 2*/
    un.setValueAttribute("[email protected]");

   /*Trial 3*/
   un.setAttribute("value", "[email protected]");

Here is the output ( first 2 lines proof that they are not nulled )

 HtmlTextInput[<input id="ap_email" name="email" value="" type="email" size="30" maxlength="128" tabindex="1" autocorrect="off" autocapitalize="off">]
HtmlPasswordInput[<input id="ap_password" name="password" type="password" maxlength="1024" size="20" tabindex="2" class="password">]
Exception in thread "main" java.lang.NullPointerException
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.hasTopCall(ScriptRuntime.java:3241)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:102)
    at com.gargoylesoftware.htmlunit.javascript.host.dom.MutationObserver.attributeReplaced(MutationObserver.java:165)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:349)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.setAttributeNS(HtmlElement.java:210)
    at com.gargoylesoftware.htmlunit.html.HtmlInput.setAttributeNS(HtmlInput.java:552)
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.setAttributeNS(HtmlTextInput.java:164)
    at com.gargoylesoftware.htmlunit.html.DomElement.setAttribute(DomElement.java:467)
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.typeDone(HtmlTextInput.java:83)
    at com.gargoylesoftware.htmlunit.html.DoTypeProcessor.typeDone(DoTypeProcessor.java:178)
    at com.gargoylesoftware.htmlunit.html.DoTypeProcessor.doType(DoTypeProcessor.java:127)
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.doType(HtmlTextInput.java:63)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.type(HtmlElement.java:548)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.type(HtmlElement.java:496)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.type(HtmlElement.java:481)
    at javaapplication8.JavaApplication8.main(JavaApplication8.java:39)

Upvotes: 1

Views: 528

Answers (2)

Ahmed Ashour
Ahmed Ashour

Reputation: 5549

With 2.25-snapshot, the test case passes.

Please note it is now HtmlEmailInput, not HtmlTextInput.

Upvotes: 2

RBRi
RBRi

Reputation: 2879

This is a known issue with HtmlUnit. Can you please verify, if the problem still exists with the latest snapshot. If yes, please report your problem to our bug tracker (if you like you can open a new one or add to https://sourceforge.net/p/htmlunit/bugs/1811/).

For setting the field you should use the type method because typing into a field does not trigger the attributeChange listener (in real browsers but HtmlUnit triggers this and the triggered method fails in your case). This is what we try to fix at the moment. Setting the value attribute is more or less a bad idea because this will trigger the attributeChange listener (like in real browsers).

Hope that helps

Upvotes: 1

Related Questions