2016-07-06 10 views
1

HtmlUnitで要素のテキストを設定しようとしていますが、何らかの理由でNullPointerExceptionがスローされていることを理解できません。明らかに要素はページ上に存在し、htmlunitによって見つけられます!なぜsetTextはエラーを投げるのでしょうか?何か案は?HtmlUnit .setText()はnullポインタ例外をスローしていますが、要素はnullではありません。

コード:

String code = "123"; 

HtmlTextInput textElem = (HtmlTextInput)page.getHtmlElementById("gc-redemption-input"); 
System.out.println(textElem); 
textElem.setText(code); 

出力:

HtmlTextInput[<input type="text" required="" id="gc-redemption-input" name="claimCode" class="gc-redemption-input a-input-text a-span12 a-form-selected" autofocus="autofocus" value="">] 

エラー:Unclosed character literal:それは私のIDE内で次のエラーを示しているため

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:348) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:353) 
    at com.gargoylesoftware.htmlunit.html.HtmlElement.setAttributeNS(HtmlElement.java:209) 
    at com.gargoylesoftware.htmlunit.html.HtmlInput.setAttributeNS(HtmlInput.java:538) 
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.setAttributeNS(HtmlTextInput.java:164) 
    at com.gargoylesoftware.htmlunit.html.DomElement.setAttribute(DomElement.java:331) 
    at com.gargoylesoftware.htmlunit.html.HtmlInput.setValueAttribute(HtmlInput.java:94) 
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.setText(HtmlTextInput.java:124) 
+1

これは修正されました。 [Htmlunit入力の入力時にnullポインタがありますが、要素がNULLになっていません]を参照してください(http://stackoverflow.com/questions/41839518/htmlunit-null-pointer-on-filling-input-but-element-is-not-nulled) – RBRi

答えて

1

問題は、String code = '123';かもしれません。代わりに二重引用符を使用してください:String code = "123";

+1

良いキャッチをありがとう!残念ながらそれは問題ではありません、それは私がコピーしていない代わりに入力した1行でした:) – k9b

関連する問題