I am getting java.lang.NullPointerException when I run the testcase in Eclipse. Can somebody help me in pointing out the error I have made.
错误@第17行:LoginPage.Java中的web元素。@第12行:tc_logtest_001 .java中的LoginPage .
**LoginPage.Java**
package com.internetBanking.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
WebDriver driver;
public LoginPage (WebDriver driver) {
this.driver = driver;
}
WebElement usrname = driver.findElement(By.name("uid"));
WebElement pwd = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.name("btnLogin"));
public void setUsrname(String uname) {
usrname.sendKeys(uname);
}
public void setPwd(String pswd) {
pwd.sendKeys(pswd);
}
public void login() {
login.click();
}
}
**TC_LoginTest_001.java**
package com.internetBanking.testCases;
import org.testng.annotations.Test;
import com.internetBanking.pageObjects.LoginPage;
public class TC_LoginTest_001 extends BaseClass {
@Test
public void LoginTest() {
driver.get(baseURL);
logger.info("URL is opened");
LoginPage loginPage = new LoginPage(driver);
loginPage.setUsrname(username);
logger.info("Username is entered");
loginPage.setPwd(password);
logger.info("Password is entered");
loginPage.login();
logger.info("Login button is clicked");
}
}
当我运行测试用例时,我得到以下错误:
java.lang.NullPointerException在com.internetBanking.pageObjects.LoginPage。(LoginPage.java: 17)com.internetBanking.testCases.TC_LoginTest_001.LoginTest (TC_LoginTest_001.java: 12)在sun.reflect.NativeMethodAccessorImpl。invoke0(本地方法)在sun.reflect.NativeMethodAccessorImpl。调用(未知源)在sun.reflect.DelegatingMethodAccessorImpl。调用(未知源)在java.lang.reflect.Method。调用(未知源)org.testng.internal.MethodInvocationHelper.invokeMethod (MethodInvocationHelper.java: 124)org.testng.internal.Invoker.invokeMethod (Invoker.java: 583)org.testng.internal.Invoker.invokeTestMethod (Invoker.java: 719)org.testng.internal.Invoker.invokeTestMethods (Invoker.java: 989)org.testng.internal.TestMethodWorker.invokeTestMethods (TestMethodWorker.java: 125)org.testng.internal.TestMethodWorker.run (TestMethodWorker.java: 109)org.testng.TestRunner.privateRun (TestRunner.java: 648)org.testng.TestRunner.run (TestRunner.java: 505)org.testng.SuiteRunner.runTest (SuiteRunner.java: 455)org.testng.SuiteRunner.runSequentially (SuiteRunner.java: 450)org.testng.SuiteRunner.privateRun (SuiteRunner.java: 415)org.testng.SuiteRunner.run (SuiteRunner.java: 364)org.testng.SuiteRunnerWorker.runSuite (SuiteRunnerWorker.java: 52)org.testng.SuiteRunnerWorker.run (SuiteRunnerWorker.java: 84)org.testng.TestNG.runSuitesSequentially (TestNG.java: 1208)org.testng.TestNG.runSuitesLocally (TestNG.java: 1137)org.testng.TestNG.runSuites (TestNG.java: 1049)org.testng.TestNG.run (TestNG.java: 1017)org.testng.remote.AbstractRemoteTestNG.run (AbstractRemoteTestNG.java: 115)org.testng.remote.RemoteTestNG.initAndRun (RemoteTestNG.java: 251)org.testng.remote.RemoteTestNG.main (RemoteTestNG.java: 77)
考虑以下代码:
public class MainTest {
public static void main(String[] args) {
new Test(new Object());
}
}
class Test {
InitMe initMe = new InitMe();
public Test(Object o){
System.out.println("Test instance has been created");
}
}
class InitMe{
public InitMe(){
System.out.println("Init me instance has been created");
}
}
如果运行它,您将看到以下输出:
Init me instance has been created
Test instance has been created
表示字段在构造函数执行之前被初始化。所以在你的例子中,你有以下结构:
public class MainTest {
public static void main(String[] args) {
new Test(new Object());
}
}
class Test {
Object o;
String oStr = o.toString();
public Test(Object o){
this.o = o;
}
}
,其中您的字段通过尚未初始化的引用进行初始化(因为尚未调用构造函数)。
你的特解应该是修改:
WebDriver driver;
public LoginPage (WebDriver driver) {
this.driver = driver;
}
WebElement usrname = driver.findElement(By.name("uid"));
WebElement pwd = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.name("btnLogin"));
:
WebDriver driver;
WebElement usrname;
WebElement pwd;
WebElement login;
public LoginPage (WebDriver driver) {
this.driver = driver;
usrname = driver.findElement(By.name("uid"));
pwd = driver.findElement(By.name("password"));
login = driver.findElement(By.name("btnLogin"));}
}