Web Driver throws nullpointer exception while trying to find Element in my application



  • public class Loginpage {
    
    public  WebDriver driver;
    
    
        By uname = By.xpath("//*[@id='UserName']");
        By nextbutton = By.id("btnNext");
        By passw = By.id("Password");
        By signinbtn = By.id("btnSubmit");
    
    
        public Loginpage(WebDriver driver){
            driver = this.driver;
    
        }
    
        public void setUserName(String uid ,String pass){
    
            driver.findElement(uname).sendKeys(uid);
            driver.findElement(nextbutton).click();
            driver.findElement(passw).sendKeys(pass);
            driver.findElement(signinbtn).click();
    
    }
    }
    

    [RemoteTestNG] detected TestNG version 6.14.2 Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 23334 Only local connections are allowed. Sep 20, 2018 4:24:48 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS java.lang.NullPointerException at Testrun.Loginpage.setUserName(Loginpage.java:24) at Testrun.Logintopage.validlogin(Logintopage.java:22) 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:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) 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:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) 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)

    @Test
      public void validlogin() throws InterruptedException {
    
          try{
          WebDriver driver = new ChromeDriver();
              //this.driver = new ChromeDriver();
          driver.get("https://qa.reventics.com/REVCDIUTROP_PROD/");
          driver.manage().window().maximize();
          Thread.sleep(2000);
          Loginpage login = new Loginpage(driver);
    
         login.setUserName("George", "Reventics@1");
    
          }catch (Exception e) {
              e.printStackTrace();
    
        }
    


  • Your assignment doesn't look proper (there might be other problems which I did not look at). Instead of:

    driver = this.driver;
    

    You need to use

    this.driver = driver
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2