0

Below is the code I am getting an java.lang.NullPointerException error

error:[RemoteTestNG] detected TestNG version 6.14.2 Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 29667 Only local connections are allowed. Jul 01, 2018 8:21:49 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS FAILED: Login java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy9.sendKeys(Unknown Source) at com.Agec.Actions.LoginPageActions.getLogin(LoginPageActions.java:20) at com.Agec.Tests.Bat_Suite.Login(Bat_Suite.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)

=============================================== Default test

Tests run: 1, Failures: 1, Skips: 0

=============================================== Default suite

Total tests run: 1, Failures: 1, Skips: 0

package com.Mil.Pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LoginPage 
  {   
@FindBy(xpath="//input[@type='text']")
public WebElement CellNo;

@FindBy(xpath="//input[@type='password']")
public WebElement Password;

@FindBy(xpath="//span[text()=Sign in")
public WebElement SignInBtn;

}


package com.Mil.Actions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import com.Mil.Pages.LoginPage;
import com.Mil.Tests.TestCaseBase;
public class LoginPageActions
  {
WebDriver driver;

LoginPage lp=PageFactory.initElements(TestCaseBase.driver, LoginPage.class);
public LoginPageActions(WebDriver driver)
{
    this.driver=driver;
}
 public void getLogin()
      {
 lp.CellNo.sendKeys("123");
 lp.Password.sendKeys("569");
 lp.SignInBtn.click();
     }
       }    


package com.Mil.Tests;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class TestCaseBase 
 {
    public  static WebDriver driver=null;
    @BeforeTest
    public void LaunchBrowser() throws Exception
     {
    Properties Prop=new Properties();
    File Fil=new File("C:\\Users\\eclipse- 
     workspace\\Mil\\TestConfig\\config.properties");
   FileInputStream fileInput=new FileInputStream(Fil);
   Prop.load(fileInput);
System.setProperty("webdriver.chrome.driver",Prop.getProperty("Chrome_Path"));
   driver=new ChromeDriver();
   driver.get(Prop.getProperty("Url"));
    }

  @AfterTest()
  public void CloseBrowser()
  {
      driver.quit();
      }
          }



package com.Mil.Tests;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import com.Mil.Actions.LoginPageActions;
public class Bat_Suite extends TestCaseBase
 {
  LoginPageActions lpa=PageFactory.initElements(TestCaseBase.driver, 
 LoginPageActions.class);
  @Test(priority=1)
   public void Login ()
     {
    lpa.getLogin();
       }
   }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Jul 02 '18 at 05:59

1 Answers1

0

Your driver is null, you're trying to call it but it's null to initialize the elements. To solve your issue you need to change @BeforeTest to @BeforeClass, even you don't need any TestNG annotations in this case.

public class TestCaseBase { 
public static WebDriver driver=null; 

@BeforeClass
public void LaunchBrowser() throws Exception { 
Properties Prop=new Properties(); 
File Fil=new File("C:\\Users\\eclipse- workspace\\Mil\\TestConfig\\config.properties"); 
FileInputStream fileInput=new FileInputStream(Fil); 
Prop.load(fileInput); 
System.setProperty("webdriver.chrome.driver",Prop.getProperty("Chrome_Path"));      
driver=new ChromeDriver(); 
driver.get(Prop.getProperty("Url")); 
}

@AfterClass() 
public void CloseBrowser() { 
driver.quit(); 
} 
} 

Recommended solution is to create get driver function with singleton that checks if the driver is null or not, if so create new driver and return.

And you must figure out the annotations types:

@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run. @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to runshortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. @AfterClass: The annotated method will be run after all the test methods in the currentclass have been run.

@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method. from TestNG documentation.

@BeforeTest works before the tag in your xml runner.

Nael Marwan
  • 1,030
  • 1
  • 13
  • 32