2013-10-03 54 views
0

我正在使用selenium webdriver作爲JSP上的應用程序。我添加的任何東西都與硒有關,它會給出錯誤。它只在刪除WebElements時才起作用。任何人有什麼想法我可以做什麼?爲什麼任何try/catch while while循環會導致空指針異常

這裏是我的代碼

while(i < x) 
{ 

    i++; 
    writer.println("\n\n"+"Number "+i +"\n\n"); 
    if(rs.next()) 
    { 

     WebElement acc = wait.until(ExpectedConditions.elementToBeClickable(By.id("x_account_"+i))); 
     WebElement desc = driver.findElement(By.id("x_original_"+i)); 
     WebElement inf_desc = driver.findElement(By.id("x_infringement_"+i)); 
     WebElement twt_url = driver.findElement(By.id("x_url_"+i)); 
     List<WebElement> link_in_twt = driver.findElements(By.name("x_target_"+i)); 
     WebElement link_in_twt_1 = driver.findElement(By.id("x_website_direct_"+i)); 
     WebElement link_in_twt_conf = driver.findElement(By.id("x_website_direct_confirm_"+i)); 
     WebElement report_another_twt = driver.findElement(By.id("more_x_link")); 

     String ACCOUNT = rs.getString("User"); 
     String DESC = "Sound and video recordings as performed by the " + 
       "artist known as "+rs.getString("artist")+"."; 

     String INF_DESC = "This account is" + 
       " incorrect."; 

     String TWT_URL = rs.getString("FB_TW_Link"); 
     //------------------------------------------------------------------- 
     try 
     { 
      acc.sendKeys(ACCOUNT); 
      String Acc = "Account: "+ ACCOUNT +" - Complete"; 
      writer.println(Acc+"\n"); 
      //output.scrollRectToVisible(null); 
      Thread.sleep(timer); 
     } 
     catch(InterruptedException e) 
     { 
      writer.println(e+" ID:14"); 
     } 
     //-------------------------------------------------------------------               
     try 
     { 
      desc.sendKeys(DESC); 
      String Desc = "Description: "+DESC+" - Complete"; 
      writer.println(Desc+"\n"); 
      Thread.sleep(timer); 
     } 
     catch(InterruptedException e) 
     { 
      writer.println(e+" ID:15"); 
     } 
     //------------------------------------------------------------------- 
     try 
     { 
      inf_desc.sendKeys(INF_DESC); 
      String Inf_Desc = "Inf_Des: "+INF_DESC+" - Complete"; 
      writer.println(Inf_Desc+"\n"); 
      Thread.sleep(timer); 
     } 
     catch(InterruptedException e) 
     { 
      writer.println(e+" ID:16"); 
     } 
     //------------------------------------------------------------------- 
     try 
     { 
      twt_url.sendKeys(TWT_URL); 
      String Twt_Url= "URL: "+TWT_URL+" - Complete"; 
      writer.println(Twt_Url+"\n"); 
      Thread.sleep(timer); 
     } 
     catch(InterruptedException e) 
     { 
      writer.println(e+" ID:17"); 
     } 
     //------------------------------------------------------------------- 
     try 
     { 
      for(WebElement RadioOption : link_in_twt) 
      { 
       if(RadioOption.getAttribute("value").equals("website")) 
       RadioOption.click(); 
       String Link_in_twt = "Radio in URL: Click - Complete"; 
       writer.println(Link_in_twt+"\n"); 
       Thread.sleep(timer);                  
      } 

     } 
     catch(InterruptedException e) 
     { 
      writer.println(e+" ID:18"); 
     } 
     //------------------------------------------------------------------- 
     try 
     { 
      link_in_twt_1.click(); 
      String Link_in_twt_1 = "Radio in URL2: Click - Complete"; 
      writer.println(Link_in_twt_1+"\n"); 
      Thread.sleep(timer); 
     } 
     catch(InterruptedException e) 
     { 
      writer.println(e+" ID:19"); 
     } 
     //------------------------------------------------------------------- 
     try 
     { 
      link_in_twt_conf.click(); 
      String Link_in_twt_conf = "Radio URL3 Conf: Click - Complete"; 
      writer.println(Link_in_twt_conf+"\n"); 
      Thread.sleep(timer); 
     } 
     catch(InterruptedException e) 
     { 
      writer.println(e+" ID:20"); 
     } 

     if(i != x) 
     { 
      //try 
      //{ 
       report_another_twt.click(); 
       writer.println("Report Another: Click - Complete"); 
      // Thread.sleep(3000); 
      //} 
      //catch(InterruptedException e) 
      //{ 
      // writer.println(e + "ID: Report Another x"); 
      //}                 
     } 
    } 
    else 
    { 
     writer.println("There was an error in the MySQL query! ID:26"); 
    } 


} 

錯誤

java.lang.NullPointerException 
at att.yet.petl.XmlServlet.doGet(XmlServlet.java:492) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
at java.lang.Thread.run(Thread.java:722) 

道歉沒有給予足夠的信息

線492

WebElement ack1 = driver.findElement(By.id("claimer"));

protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { 

    MysqlConnect(); 

    PrintWriter writer = response.getWriter(); 

     try 
     { //note that the entire script is basically in one try/catch  
     int x =1; 
     int i = 0; 
     rs = st.executeQuery(" query is here"); 


      DesiredCapabilities capability = DesiredCapabilities.firefox(); 
      capability.setCapability("platform", Platform.ANY); 
      capability.setCapability("binary", "/ms/dist/fsf/PROJ/firefox/16.0.0/bin/firefox"); 

      URL url2 = null; 
      try { 
       url2 = new URL("http://localhost:4444/wd/hub"); 
      } catch (MalformedURLException e1) { 
       e1.printStackTrace(); 
      } 

      WebDriver driver = new RemoteWebDriver(url2,capability); 

      driver.get("https://site.com"); 

     WebDriverWait wait = new WebDriverWait(driver,10); 

      int timer = 0; 

       WebElement numOne = driver.findElement(By.id("owner")); 
       WebElement numTwo = driver.findElement(By.id("input_text")); 
       WebElement numThree = driver.findElement(By.id("comp")); 

一切都在這之間僅僅是點擊和的SendKeys

+0

呃,格式化你的代碼,並告訴我們你的'XmlServlet#的doGet()'方法。 –

+5

並指出哪一行代碼是XmlServlet.java的第492行 –

+1

您可以在您的代碼中添加註釋以向我們展示哪一行是拋出NullPointerException?如果您發佈整個班級代碼,這也可能有所幫助。我懷疑你的WebDriver可能是空的。 –

回答

1

我想這條線,根據文檔引發NullPointerException異常

if(RadioOption.getAttribute("value").equals("website")) 

的一個try/catch:http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute%28java.lang.String%29

的java.lang .String getAttribute(java.lang.String name)
.........
Returns: 如果未設置該值,則該屬性的當前值爲或爲空

如果未設置屬性,則該函數返回null和整個表達式拋出NullPionterException。

試試這個:

if("website".equals(RadioOption.getAttribute("value")) 
+0

優秀的趕上!謝謝! –

相關問題