0

Somebody please explain why am getting null pointer exception

HtmlPage page = null;
boolean savePagesLocally = false;
String url = "http://example.com";

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
try
{
    page = webClient.getPage( url );

    HtmlRadioButtonInput radioButton2 = (HtmlRadioButtonInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_rdoIndvl");
    radioButton2.click();

    HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
    textField3.setValueAttribute("1061726"); // null pointer occurs here!
4

1 回答 1

0

For those who would like to know: Replacing the line

HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");

With the following loop:

HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
while(textField3 != null) {
  webClient.waitForBackgroundJavaScript(100)
  textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
}

This loop will wait until HTMLUnit has an element by that ID.

于 2013-04-13T10:19:55.707 回答