2017-04-14 71 views
-1

我不知道如何解決這個空指針異常。我的想法是頁面屬性沒有找到,因爲頁面被加載,這是造成這種情況。如果有人能夠指出這將有所幫助。提前致謝。java硒空指針

代碼:

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class Flight { 

    public static WebDriver driver; 

    //This following section is for browser and getting the url 
    public static WebDriver browser(){ 


     System.setProperty("webdriver.chrome.driver", "C:\\Users\\chq-sheikhr\\Downloads\\eclipse\\chromedriver.exe"); 
     WebDriver driver= new ChromeDriver(); 

     driver.get("https://www.orbitz.com/Flights"); 
     return driver; 

    } 

    //this following section is getting the properties of the page 
    public static void getPageProperties(String ff,String ft, String fd, String rd){ 

     //Flight f= new Flight(); -- I thought I was getting null pointer because properties were not found 
     //f.browser(); -- putting them here is how these webelements would be found and null pointer issue will be solved but NO 
     WebElement flyFrom= driver.findElement(By.id("flight-origin")); 
     WebElement flyTo= driver.findElement(By.id("flight-destination")); 
     WebElement flyDate= driver.findElement(By.id("flight-departing")); 
     WebElement returnDate= driver.findElement(By.id("flight-returnin")); 
     WebElement flight_search_btn= driver.findElement(By.id("search-button")); 

     flyFrom.sendKeys(ff); 
     flyTo.sendKeys(ft); 
     flyDate.sendKeys(fd); 
     returnDate.sendKeys(rd); 
     flight_search_btn.click(); 

    } 


    // this following section will have the arguments that we will provide for flight search 
    public static void main (String [] args){ 

     Flight f= new Flight(); 
     f.browser(); 
     f.getPageProperties("MSP", "SEA", "05/01/2017", "05/05/2017"); 

    } 


} 

錯誤:

Only local connections are allowed. 
Exception in thread "main" java.lang.NullPointerException 
    at Flight.getPageProperties(Flight.java:27) 
    at Flight.main(Flight.java:47) 
+0

能ÿ ou編輯您的原始文章,幷包括有問題的HTML部分?首先,你可能拼錯了「返航」,但這可能不是問題,因爲它在第一個findElement語句中報告了空例外。這可能非常簡單,因爲您尚未爲您的瀏覽器(驅動程序)聲明隱式等待時間,因此它可能試圖在加載完成之前讀取Web元素。 –

+0

可能的重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – JeffC

回答

-1

你必須改變

WebDriver driver= new ChromeDriver(); 

driver= new ChromeDriver(); 
+0

謝謝,就是這樣。 –