user1145782
user1145782

Reputation: 3

Calling a method in another class causes NullPointerException

I am new to Java so please bear with me. I am using Java and Selenium to test a web application.

Basically, what I am trying to do is read in data from a cvs file in Class Main and then once I have the data, calling the login method in another class passing through the username and password variables read from the cvs file.

Maybe I am calling the function incorrectly, I am not sure but the line I get the NPE on is

driver.findElement(By.name("j_username")).clear(); 

in the Login function in the LiveProcess Class. If I move the Login method into the Main class I do not have any problems. I would appreciate any help you can give me - thanks in advance!

Main Class

public void ReadTestData() throws Exception
{
//Open CVS File and extract the contents
try {
CsvReader testData = new CsvReader("C:\\Selenium\\TestData.csv");   
testData.readHeaders();
while (testData.readRecord())
{
String testType = testData.get("TestType");
String testName = testData.get("TestName");
String testDescription = testData.get("Description");
String userName = testData.get("UserName");
String password = testData.get("Password");
String firstName = testData.get("FirstName");
String lastName = testData.get("LastName");
int testTypeInt = Integer.parseInt(testType);
RunTests();
}   
testData.close();

} 
catch (FileNotFoundException e) 
{
e.printStackTrace();
} 
catch (IOException e) {
e.printStackTrace();
}
}

public void RunTests() throws Exception
{
switch(testTypeInt)
{
case 1:
LiveProcessTests method = new LiveProcessTests();
method.Login(userName, password);
break;

default:
break;
}
}

LiveProcess Class

public void Login(String uName, String pWord) throws Exception 
{
System.out.println("USER NAME: " + uName);
System.out.println("USER NAME: " + pWord);

driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).click();
driver.findElement(By.name("j_username")).sendKeys(uName);

}

Actual Error:

java.lang.NullPointerException
at com.testscripts.LiveProcessTests.Login(LiveProcessTests.java:31)
at com.testscripts.Main.RunTests(Main.java:97)
at com.testscripts.Main.ReadTestData(Main.java:75)
at com.testscripts.Main.InitializeTests(Main.java:44)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Upvotes: 0

Views: 1850

Answers (1)

Adel Boutros
Adel Boutros

Reputation: 10295

Most likely driver.findElement(By.name("j_username")) is null which means it is unable to find the element you are searching.

Make sure the name is correct and that the element exist. Also You have to initialise the driver but it is not seen in your code

Example:

if (driver.findElement(By.name("j_username")) != null ) {
    driver.findElement(By.name("j_username")).clear();
    driver.findElement(By.name("j_username")).click();
    driver.findElement(By.name("j_username")).sendKeys(uName);
}

UPDATE:

You should initialize your driver. Something like:

driver = new FirefoxDriver();

Upvotes: 6

Related Questions