有人请解释为什么会出现空指针异常
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!
答案 0 :(得分:0)
对于那些想知道的人: 替换行
HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
使用以下循环:
HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
while(textField3 != null) {
webClient.waitForBackgroundJavaScript(100)
textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
}
此循环将等到HTMLUnit具有该ID的元素。