-2

Main program

public class signin{

    public static void main(String[] args) throws Exception {

    util.OpenBrowser();

    b.closebrowser();


}

1st method is in class util: openbrowser is one method

public class util{

public static WebDriver OpenBrowser(){

    System.setProperty("webdriver.chrome.driver", 
                       "D:\\Selenium\\selenium3.1\\chromedriver.exe");

    driver = new ChromeDriver();

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

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

}

2nd method(closebrowser) is in another class b

public class b  {


    static WebDriver driver;

    public static void Execute(WebDriver driver){
        homepage.phonenumber(driver).sendKeys(constant.Phonenumber);
        homepage.proceedbutton(driver).click();
    }


     public static void closebrowser(){ 
        driver.close();
     }

}

I am trying to call 2 methods of different classes in another class and I am getting null pointer exception and pointing the line to closebroweser method. Why null pointer exception occured while calling the second method?

closebrowser method is working only when I am not calling openbrowser method from different class. It is working if i am writing openbrowser code in main program itself.

return
  • 291
  • 1
  • 13
  • 2
    You never instantiated `static WebDriver driver;` in class `b`, thus it has the value `null`. calling a method on an `null` object (`driver.close()`) leads to a `NullPointerException`. Some remarks on your code: --- In Java, class names should always start with an uppercase letter (`signin` -> `siginin`), while methods should start with a lowercase letter (`OpenBrowser` -> `openBrowser`) --- Method names should alway be written in in camelCase (`closebrowser()` -> `closeBrowser()`) --- Making every 2nd line a blank line does not improve the readability of your code. – Turing85 Jun 02 '18 at 08:10
  • 1
    One addition to @Turing85's suggestion on naming conventions: `constant.Phonenumber` should be `constant.PHONE_NUMBER` (contants name should be in uppercase, separated by underscore). More information can be founde here: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html – return Jun 02 '18 at 08:15
  • Adendum to my last comment: class `signin` shoudl be renamed to `Signin`... why can we edit comments for only five minutes? -.- – Turing85 Jun 02 '18 at 08:25

1 Answers1

0

It seems that you missed driver instantiation in class b. You should add method to class b to set instantiate driver. and call it in method main.

public class b  {
    static WebDriver driver;

    public static void setDriver( WebDriver driver){
        b.driver = driver;
    }

    public static void Execute(WebDriver driver){

        homepage.phonenumber(driver).sendKeys(constant.Phonenumber);
        homepage.proceedbutton(driver).click();

    }


    public static void closebrowser(){  
        driver.close();         
    }
}

call method setDriver:

public static void main(String[] args) throws Exception {
    WebDriver driver = util.OpenBrowser();
    b.setDriver(driver);
    b.closebrowser();
}
Esterlinkof
  • 1,444
  • 3
  • 22
  • 27