0

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
  • 1
    Voting to close. I'm not sure how this sort of questions are going to help other users. – dzieciou May 9 at 14:59
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();
| improve this answer | |
  • Really Thanks.. It helped, only a single line change..!! – Danny Rusty Aug 29 '19 at 10:54
0

this worked for me too i declared driver outside of the method and again in method that why it through me nullpointer exception after removing duplicate element it worked for me.

package TestNGPackage;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

public class BasicTestNG {

WebDriver driver;

@BeforeMethod

public void setup() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "Tools/chromedriver.exe"); driver= new ChromeDriver(); driver.get("https://www.google.com/"); Thread.sleep(1000); driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); }

@Test
public void mytest()
{
    String S1= driver.getTitle();
    System.out.println("Title of page"+ S1);
}
@AfterMethod
public void teardown()
{
    driver.close();;
    //driver.manage().deleteAllCookies();

}

}

| improve this answer | |
0

in your code at the @BeforeTest section

change : WebDriver driver = new ChromeDriver(); this to driver = new ChromeDriver();

This will solve the problem.enter code here

////////////////

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
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);

| improve this answer | |

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