1

I am having the below selenium structure and i am facing error after browser is launched & maximised. When the @Test method is starting to execute , i am getting Java null pointer exception.

Please suggest a solution

Base Class:

public class BaseClass

{


    public static WebDriver driver ;

    public BaseClass(WebDriver _driver)
    {
        driver = _driver;
    }

    public  WebElement FirstName = driver.findElement(By.name("firstname"));
    public  WebElement LastName  = driver.findElement(By.name("LastName"));




    public String GetFirstName()
    {
        return FirstName.getText();

    }

    public String GetLastName()
    {
        return LastName.getText();
    }


    public WebDriver  SetFirstname(String FirstName)
    {
        this.FirstName.sendKeys(FirstName);
        return driver;
    }

    public WebDriver SetLastname(String LastName)
    {
        this.LastName.sendKeys(LastName);
        return driver;
    }

}

Test Class:

public class TestClassTest  

{

     public static WebDriver driver =null;
     private String Url ="";
     private String DriverOption="";
     private String DriverPath="";

     private String Firstname= "";
     private String LastName= "";



    @BeforeSuite(alwaysRun = true)
    public void setupBeforeSuite(ITestContext context) throws IOException
     {

     Url = context.getCurrentXmlTest().getParameter("URL");

     DriverOption = context.getCurrentXmlTest().getParameter("DriverOptions");

     DriverPath = context.getCurrentXmlTest().getParameter("DriverPath");


     Firstname= ReadValue_XL(driver,0,0);

     LastName= ReadValue_XL(driver,0,1);

     }

    @Parameters("Browser")
    @BeforeTest
    public void SetUp(String Browser) 
    {
        if(Browser.equalsIgnoreCase("chrome"))  
        {
        System.setProperty(DriverOption,DriverPath);

        driver = new ChromeDriver();
        }

        if(Browser.equalsIgnoreCase("firefox"))  
        {
        System.setProperty(DriverOption, DriverPath);

        driver = new FirefoxDriver();
        }

        driver.get(Url);

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

        driver.manage().window().maximize();



    }


    @Test (priority = 1)
    public void TestOne()
    {
        BaseClass Base= new BaseClass(driver);  

        Base.SetFirstname(Firstname);
        Base.SetLastname(LastName);

        Assert.assertEquals(Base.GetFirstName(), "Dog Food");


    }



    @AfterTest
    public void Driver_close() 
    {
        driver.close();
        driver.quit();
    }


    public String ReadValue_XL(WebDriver driver,int Row,int Column) throws IOException
    {
        XSSFWorkbook WrkBk = new XSSFWorkbook(".\\Input\\Input.xlsx");

        XSSFSheet sourceSheet = WrkBk.getSheet("Sheet1");

        XSSFRow sourceRow = sourceSheet.getRow(Row);

        XSSFCell cell1=sourceRow.getCell(Column);

        cell1.toString();

        WrkBk.close();

        return cell1.toString();

    }
}

Error:

java.lang.NullPointerException
    at baseProject.BaseClass.<init>(BaseClass.java:19)
    at TestProject.TestClassTest.TestOne(TestClassTest.java:88)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:776)
    at org.testng.TestRunner.run(TestRunner.java:634)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
    at org.testng.SuiteRunner.run(SuiteRunner.java:334)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
    at org.testng.TestNG.runSuites(TestNG.java:1161)
    at org.testng.TestNG.run(TestNG.java:1129)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

I am able to understand that driver is not properly parsed or initialised in base class. Please suggest where i have to resolve this issue.

When i extend base class in testclass , no TestNG classes are running.

sriram
  • 51
  • 7
  • 4
    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) – rmlan Oct 18 '17 at 16:59

2 Answers2

2

Initialisers are invoked before constructors and hence, NullPointerException, try the below code:

public class BaseClass{

  public static WebDriver driver;

  public BaseClass(WebDriver _driver){
      driver = _driver;
      FirstName = driver.findElement(By.name("firstname"));
      LastName  = driver.findElement(By.name("LastName"));
  }

  public  WebElement FirstName;
  public  WebElement LastName;
  //Rest of the code
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
1

@DarshanMehta has well explained the problem and its solution. I have just tested it and it works. I would also add:

The problem you are facing has been well discussed in Stackoverflow. I think this link can give you a better understanding of the order of execution when a class is instanciated: Are fields initialized before constructor code is run in Java?

Thus you can read in the first answer that initialization block is done after the super() part of the constructor, BUT before the rest of the constructor.

you would not have had this error if, for exemple, your driver was initialised in a superclass of BaseClass. Therefore, if you really want to initialized FirstNameand LastName in initialization block, you can create a superclass from which BaseTest will inherits. And it should solve your issue.

you can try the following code, that illustrates it: create a super class:

public class MotherClass {
    public MotherClass(WebDriver _driver){
        driver = _driver;
    }

    WebDriver driver;
}

and add super in the constructor of BaseClass, that inherits from MotherClass:

public class Testasupp extends MotherClass{    
    public BaseClass(WebDriver _driver)
        {
            super(_driver);
            FirstName = driver.findElement(By.name("firstname"));
            LastName  = driver.findElement(By.name("LastName"));
        }

To conclude: Except if you have good reason to prefer this solution, I really think @DashanMehta gives a better solution.

Nathan Ripert
  • 872
  • 1
  • 7
  • 18