1

I'm using below code using Testng, getting error Null Pointer exception:

public class Login_SF {

public static WebDriver driver;
String text;

@BeforeTest
 public void Login() {

  System.setProperty("webdriver.chrome.driver", "C:\\BobbyReddy\\chromedriver_win32\\chromedriver.exe");

    //Launch Browser
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    //Get site and do Login
    driver.get("https://login.salesforce.com/");
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    driver.findElement(By.name("username")).sendKeys("xxxxxx");
    driver.findElement(By.id("password")).sendKeys("xxxxxxx");
    driver.findElement(By.id("Login")).click();
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    //Verify page title
    String pageTitle = driver.getTitle();
    Assert.assertEquals("Home | Salesforce", pageTitle);
}   
@Test(priority=2)
public void Marketingtab() throws InterruptedException{

driver.findElement(By.xpath("//button[contains(@class,'salesforceIdentityAppLauncherHeader')]")).click();
Thread.sleep(10000);    
driver.findElement(By.xpath("//*[@title='Marketing']")).click();
Thread.sleep(10000);

Error: FAILED: Marketingtab java.lang.NullPointerException at salesforce.Login_SF.Marketingtab(Login_SF.java:41) 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:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

  • Your error is from line 41. What is line 41 in your code? We can't tell from the snippet. Also, you only need to set the implicit wait once, unless you want to change the value from 100 to something else. – kirbycope May 3 '17 at 15:21
4

The problem seems to be in the declaration and usage of the variable driver. Although there is a class-scope variable driver, the Login() method stores the created WebDriver object in a method-scope variable. This means, that the class-scope variable driver is never set, resulting in NullPointerException when it is referenced.

To fix this, change the beginning of the Login() method:

//Launch Browser
driver = new ChromeDriver();

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.