-2
    enter code here
import java.util.ArrayList;

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


public class ArrayLiist {

    public static void main(String[] args) {
        launchBrowser("CH");
        WebDriver driver=null;
driver.findElement(By.id("user_login")).sendKeys("admin");
        driver.findElement(By.id("user_pss")).sendKeys("demo123");
        driver.findElement(By.id("wp-submit")).click(); 


    }



    public  static void launchBrowser(String bn) {
        if(bn=="CH")
        {
        System.setProperty("webdriver.chrome.driver","E:\\Selenium Downloaded\\chrome\\chromedriver.exe");
         ChromeDriver driver = new ChromeDriver();
        driver.get("http://demosite.center/wordpress/wp-admin/plugins.php");
        }


}
}

i think driver is not being identified ? i have institiated webdriver =null , still it is showing null pointer exception

Exception in thread "main" java.lang.NullPointerException at ArrayLiist.main(ArrayLiist.java:13)

1 Answers1

0

You never initialize Driver with an object, You initialize it only with null

WebDriver driver=null;

That's why you get the NPE.

Declare it as a global variablie not as local Variable:

WebDriver driver=null;

   public static void main(String[] args) {
        launchBrowser("CH");
        driver.findElement(By.id("user_login")).sendKeys("admin");
        driver.findElement(By.id("user_pss")).sendKeys("demo123");
        driver.findElement(By.id("wp-submit")).click(); 



}



public  static void launchBrowser(String bn) {
    if(bn.equals("CH"))
    {
    System.setProperty("webdriver.chrome.driver","E:\\Selenium Downloaded\\chrome\\chromedriver.exe");
     driver = new ChromeDriver();
    driver.get("http://demosite.center/wordpress/wp-admin/plugins.php");
    }

}

And use the equals method to compare strings

Jens
  • 67,715
  • 15
  • 98
  • 113