0

It works fine when I run the test class by using the all objects form PartnerLogin POM class to Test1 Class. The problem goes when I try to call login.Login(); which contains all the objects form PartnerLogin POM class method (from another class) and calling profile object right after, getting to the null pointer exception error:

java.lang.NullPointerException at page object.PartnerNavigationDrawer.Profile(PartnerNavigationDrawer.java:30)

How can I solve this?

POM class

package pageObject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class PartnerNavigationDrawer {

    public WebDriver driver;

    public PartnerNavigationDrawer(WebDriver driver) {
        // TODO Auto-generated constructor stub
        this.driver = driver;
    }

    By Dashboard = By.cssSelector("a[href='partner_dashboard.php']");

    By Profile = By.cssSelector("a[href='partner_page.php']");

    public WebElement Dashboard() {

        return driver.findElement(Dashboard);

    }

    public WebElement Profile() {

        return driver.findElement(Profile);
    }

}

Test Class

   package com.rslsolution;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import mainTest.PartnerLoginTest;
import com.rslsolution.Base;

import pageObject.PartnerLogin;
import pageObject.PartnerNavigationDrawer;

public class Test1 extends Base{

    String baseUrl = System.getProperty("user.dir");
    Properties pro = new Properties();
    FileInputStream fis;
     WebDriver driver;


    @Test
    public void testMethod() throws InterruptedException, IOException
    {
        fis =new FileInputStream(baseUrl+"\\DataDriven\\DataDriven.properties");
        pro.load(fis);

//      driver = initializeBrowser();
//      driver.get(pro.getProperty("appUrl"));
        PartnerLoginTest partnerLogin=new PartnerLoginTest();

        Thread.sleep(3000);
         partnerLogin.Login();
        Thread.sleep(3000);
//      PartnerLogin login=new PartnerLogin(driver);
//      Thread.sleep(3000);
//      login.Partner_Login().click();
//      Thread.sleep(3000);
//      login.email().sendKeys(pro.getProperty("userName"));
//      login.password().sendKeys(pro.getProperty("partnerPassword"));
//      login.Login_Button().click();
        PartnerNavigationDrawer navigationDrawer = new PartnerNavigationDrawer(driver);
        navigationDrawer.Profile().click();



        System.out.println("Checking git push command");

} 
}

Error Log

java.lang.NullPointerException
    at pageObject.PartnerNavigationDrawer.Profile(PartnerNavigationDrawer.java:28)
    at com.rslsolution.Test1.testMethod(Test1.java:35)
    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)

PartnerLogin Class

package pageObject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class PartnerLogin {
    WebDriver driver;

    public PartnerLogin(WebDriver driver) {

        this.driver = driver;
    }

    By Partner_Login = By.xpath("/html/body/div[3]/p/a[3]/b/span");

    By email = By.cssSelector("input[id='userName']");

    By password = By.cssSelector("input[id='pass']");

    By Login_Button = By.cssSelector("button[id='submit']");

    public WebElement Partner_Login() {

        return driver.findElement(Partner_Login);

    }

    public WebElement email() {


        return driver.findElement(email);
    }

    public WebElement password() {


        return driver.findElement(password);
    }

    public WebElement Login_Button() {


        return driver.findElement(Login_Button);
    }

}
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Tester
  • 37
  • 4
  • 2
    I assume driver is still `null`, since if has not been set before and you try to access this object in your `Profile()` method. I would suggest to use a debugger for questions like that first. – Sebastian Werk Feb 28 '20 at 22:01
  • Make sure the driver is set in the `Base` class, use a `BeforeMethod` – lauda Mar 01 '20 at 18:21

1 Answers1

0

driver is null in class Test1. I cannot see where you assign a value to it.

Stefan
  • 1,645
  • 1
  • 10
  • 15
  • How to assign the value to the driver in the class Test1? – Tester Feb 28 '20 at 19:43
  • ```test = whatever```. You should find out, where to get the driver from or how to crete it. See https://wiki.saucelabs.com/display/DOCS/Getting+Started+with+Selenium+for+Automated+Website+Testing#GettingStartedwithSeleniumforAutomatedWebsiteTesting-CreatinganInstanceoftheWebDriverInterface – Stefan Feb 28 '20 at 19:56