1

I am new to selenium, can someone help on the below issue. I am using page object model

Page code:

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import wrappers.CFAProject;

public class Login extends CFAProject 
{
    public static WebDriver driver;
    //  WebDriver driver = new InternetExplorerDriver();

    public Login(WebDriver driver)
    {
        Login.driver = driver;
    }
    public Login Loginapplcn(WebDriver driver, String Uname)
    {

        try 
        {
                driver.findElement(By.name("userId")).sendKeys(Uname); //--> error line
        } 
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return this;

    }
}

Test case code:

package testcases;


import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import pages.Login;
import wrappers.CFAProject;

public class TestCase01 extends CFAProject 
{
    public static WebDriver driver;
    Login loginpage;
    public String dataSheetName;

    @BeforeClass
    public void getValue()
    {
        dataSheetName ="Sheet1";
        TestCase = "ReportsPage";
        testcasediscription = "Navigating to Reports page"; 
        author = "Anu";
        cat ="test";
    }
    @Test(dataProvider = "test")

    public void TC01(String Uname, String date, String todate) throws InterruptedException
    {

        loginpage = new Login(driver);
        loginpage.Loginapplcn(driver, Uname)
            .Clickproceed()
            .Clickcommitment()
            .ClickReport()
            .clickApplication()
            .selectCreationDate(date)
            .selectToDate(todate)
            //  .selectCreationDatetry(year, month, dt);
            .clickRunReport();
    }

    public void onStart(ITestContext iTestContext) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public long takeSnap() 
    {
        // TODO Auto-generated method stub
        return 0;
    }

}

Error:

<pre>
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 31461
Only local connections are allowed.
Jan 31, 2018 5:33:16 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
java.lang.NullPointerException
at pages.Login.Loginapplcn(Login.java:17)
at testcases.TestCase01.TC01(TestCase01.java:30)
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:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
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)
java.lang.NullPointerException
at pages.Login.Clickproceed(Login.java:29)
at testcases.TestCase01.TC01(TestCase01.java:31)
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:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
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)
FAILED: TC01("502347", "25-May-2017", "25-Dec-2017")
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:72)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:46)
at pages.CFAPage.Clickcommitment(CFAPage.java:20)
at testcases.TestCase01.TC01(TestCase01.java:32)
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:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
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)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@d94a8b: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@df4efb: 46 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@fb434: 16 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@12fad11: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@108b2d7: 94 ms
</pre>
2

Your driver has not been instantiated.

Here in your test code

public void TC01(String Uname, String date, String todate) throws InterruptedException{

    loginpage = new Login(driver); // Here you are trying to propagate null reference to the constructor
    loginpage.Loginapplcn(driver, Uname)
    .Clickproceed()
    .Clickcommitment()
    .ClickReport()
    .clickApplication()
    .selectCreationDate(date)
    .selectToDate(todate)
//  .selectCreationDatetry(year, month, dt);
    .clickRunReport();
}

you're propagating driver to Login constructor. However earlier in the code you only declare that you are going to use driver

public static WebDriver driver;

but not create the object. Therefore you have NPE

1
  • 1
    Check if the solution is working for you and feel free to mark the question as correct if it is, or provide the feedback so that we can elaborate more suitable solution.
    – Alexey R.
    Jan 31 '18 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.