1

I've written my code in Java using Selenium. When I run the code, it's throwing a NullPointerException. Check the exception below

Exception in thread "main" java.lang.NullPointerException
    at AdminInterface.loginApplication(AdminInterface.java:17)
    at AdminInterface.main(AdminInterface.java:29)

My code is as follows:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class AdminInterface {
    public WebDriver driver;
    public void launchApplication() throws Exception
    {
        System.setProperty("webdriver.ie.driver", "C:\\Users\\rprem\\Downloads\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
        driver.get("https://www.gcrit.com/build3/admin/");
    }
    public void loginApplication(String Username, String Password)
    {
        driver.findElement(By.name("username")).sendKeys(Username);
        driver.findElement(By.name("password")).sendKeys(Password);
        driver.findElement(By.id("tbd1")).click();
    }
    public void closeBrowser()
    {
        driver.close();
    }
    public static void main(String[] args) 
    {
        AdminInterface obj = new AdminInterface();
        obj.loginApplication("admin", "admin@123");
    }
    }
Tom
  • 1,387
  • 3
  • 19
  • 30
  • 1
    You haven't called `launchApplication` method, where you actually initialize the webdriver object. – Barney Aug 03 '17 at 03:43
  • 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 Aug 03 '17 at 05:03

1 Answers1

0

You are seeing a NullPointerException because from main() you are trying to access the loginApplication() method right in the begining, which requires an active instance of the WebDriver i.e. the driver to findElement(By.name("username")); & findElement(By.name("password")); and perform sendKeys() method on the HTML DOM.

The solution would be to first access the launchApplication() method so you have an active instance of driver and IE Browser. Next you can access loginApplication() method.

Here is your working code block:

package demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Q45474353_NPE 
{

    public WebDriver driver;
    public void launchApplication()
    {
        System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
        driver.get("https://www.gcrit.com/build3/admin/");
    }

    public void loginApplication(String Username, String Password)
    {
        driver.findElement(By.name("username")).sendKeys(Username);
        driver.findElement(By.name("password")).sendKeys(Password);
        driver.findElement(By.id("tbd1")).click();
    }

    public void closeBrowser()
    {
        driver.close();
    }

    public static void main(String[] args) 
    {
        Q45474353_NPE obj = new Q45474353_NPE();
        obj.launchApplication();
        obj.loginApplication("admin", "admin@123");
        obj.closeBrowser();
    }

}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352