java htmlunit按钮单击无法在for循环中返回NullPointerException

时间:2016-08-26 09:09:02

标签: java nullpointerexception htmlunit buttonclick

       for(int i=0;i<=5;i++){
            HtmlPage currentPage = (HtmlPage)client.getPage("http:// http://testsite.com ");
            HtmlForm form = (HtmlForm) currentPage.getElementById("kelimeAraID");
            HtmlButton button = (HtmlButton) form.getElementsByTagName("button").get(0);
            HtmlTextInput kullaniciAdi= form.getInputByName("kelime");
            kullaniciAdi.setText(arrayList.get(i));
            HtmlPage sayfa2= button.click();
            HtmlTableRow tr=(HtmlTableRow)sayfa2.getFirstByXPath("//*[@id=\"hor-minimalist-a\"]/tbody/tr[1]");
            System.out.println(arrayList.get(i)+":"+tr.asText()); /*line:95*/
            client.closeAllWindows();
        }

线程“main”java.lang.NullPointerException中的异常     在htmlunit.HtmlUnit.main(HtmlUnit.java:95)

1 个答案:

答案 0 :(得分:0)

这可能意味着在错误情况下arrayList短于6。

试试这个:

if(i < arrayList.size()){
  System.out.println(arrayList.get(i)+":"+tr.asText());
}else {
  System.out.println("XXX":"+tr.asText());
}