-1

yesterday my test works ok, bust i want to use testng and then appeared a problem. My main class leadTest:

public class leadTest {
    WebDriver driver;
  @Test
  public void f() {


 .. 
...
...
            /*------------------------Go to leads page-------------------------------*/
        LeadsPage ldsP = new LeadsPage(driver);

        dbp.gotoLeadsPage();

        ldsP.findLeadByName("nameToFind");

    }


  @BeforeClass
  public void beforeClass() {
        driver = new FirefoxDriver();
        driver.get("http://getbase.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  }

and my class LeadsPage:

public class LeadsPage {
    WebDriver driver;
String expStatus = "new";

@FindBy(id="leads-new")
WebElement addLead;

@FindBy(className = "lead-status")
WebElement UserStatus;


public void addNewLead(){
    addLead.click();
}

public void checkUsrStat(){
    String stat = UserStatus.getText().toLowerCase();
    Assert.assertEquals(stat, expStatus.toLowerCase());

}

public void findLeadByName(String leadName){

    driver.findElement(By.partialLinkText(leadName)).click();       
}

public LeadsPage(WebDriver driver){
        PageFactory.initElements(driver, this);

}

I have problem with this part from above:

public void findLeadByName(String leadName){

    driver.findElement(By.partialLinkText(leadName)).click();       
}

I have error

    AILED: f
java.lang.NullPointerException
    at leadTest.LeadsPage.findLeadByName(LeadsPage.java:38)
    at leadTest.leadTest.f(leadTest.java:101)
    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:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

When i use

@FindBy(partialLinkText="nameToFind")
WebElement ntf;

and

ntf.click();

its works, but i cant do that, becouse in place nameToFind will be variable.

And i dont know what is wrong... Please help me

ciupakabrans
  • 45
  • 1
  • 2
  • 7
  • Where did you define and how did you initialize ldsP? – Lajos Arpad Oct 03 '15 at 10:53
  • its before in cutted part of code 'LeadsPage ldsP = new LeadsPage(driver);' I added int now to post up – ciupakabrans Oct 03 '15 at 10:56
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Louis Oct 03 '15 at 13:50

1 Answers1

1

Driver variable not initialized: you try to call findElement() method on a null object.

One way to confirm that is to print the driver value:

System.out.println("driver=" + driver); just before the exception is thrown

public void findLeadByName(String leadName){

System.out.println("driver=" + driver);
    driver.findElement(By.partialLinkText(leadName)).click();       
}

also i am noticing ,

LeadsPage ldsP = new LeadsPage(driver);

        dbp.gotoLeadsPage();

        ldsP.findLeadByName("nameToFind");

gotoLeadsPage(); and findLeadByName(); both functions are in same class LeadsPage but using different objects dbp and ldsp

May be below changes will work.

in LeadsPage class change your findLeadByName(); function as follows,

public void findLeadByName(WebDriver driver, String leadName){

    this.driver.findElement(By.partialLinkText(leadName)).click();       
}

and in your class leadTest call function findLeadByName() as follows,

LeadsPage ldsP = new LeadsPage(driver);
ldsP.findLeadByName(driver ,"nameToFind");
Mona
  • 342
  • 1
  • 3
  • 17