2017-04-14 70 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

谢谢,就是这样。 –