Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

selenium - NullPointerException in my code. How to deal with it

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");
    }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...