0

I have written the below code and getting the java null pointer exception. please help what is the problem.

public class testngbasics {

  WebDriver driver;

     @BeforeMethod
      public void setbrowser() {

            System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
             WebDriver driver= new ChromeDriver();
             driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
             driver.get("http://demo.guru99.com/v4/");
             driver.manage().deleteAllCookies();
             driver.manage().window().maximize();
             driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);

     }

     @Test
      public void login1() {

         driver.findElement(By.xpath("//input[@name='uid']")).sendKeys("mngr212595");
         driver.findElement(By.xpath("//input[@name='password']")).sendKeys("EgebYpy");
         driver.findElement(By.xpath("//input[@value='LOGIN']")).click();
         System.out.println(driver.getTitle());
     }

     @AfterMethod

     public void closebrowser()
     {

         driver.close();
     }
}

I am getting the error:

java.lang.NullPointerException it is only going to @beforemethod annotation and not going to @test and @aftermethod

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
priyanka
  • 11
  • 1
  • 1
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JeffC Jul 31 '19 at 21:24

1 Answers1

0

As you have already defined a global instance of WebDriver as driver in the line:

WebDriver driver;

You don't have to create another instance of WebDriver again as in:

WebDriver driver= new ChromeDriver();

And you need to replace this line as:

driver = new ChromeDriver();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352