-1

I am not sure how to solve this null pointer exception. My thought was the page properties are not found as the page gets loaded which is causing this. If some one could kindly point out that would be helpful. Thanks in advance.

Code:

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");

    }


}

Error:

Only local connections are allowed.
Exception in thread "main" java.lang.NullPointerException
    at Flight.getPageProperties(Flight.java:27)
    at Flight.main(Flight.java:47)
Sheikh Rahman
  • 895
  • 3
  • 14
  • 29
  • Can you edit your original post and include the section of HTML in question? For one thing, you might have mis-spelled "flight-returning" but that is likely not the problem since it reports the null exception on the very first findElement statement. It might be as simple as you have not declared an implicit wait time for your browser (driver) so it might be attempting to read web elements before they are finished loading. – Bill Hileman Apr 14 '17 at 17:21
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JeffC Apr 14 '17 at 18:19

1 Answers1

-1

You have to change

WebDriver driver= new ChromeDriver();

to

driver= new ChromeDriver();
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46