-3

Hi I am receiving following error can someone help me out with debugging below code,

package testngpackg;

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.BeforeMethod;
import org.testng.annotations.Test;

public class ARXNewTest {

     ChromeDriver driver;
    @BeforeMethod
    public void set()   {
    //ProfilesIni profile = new ProfilesIni();
    //FirefoxProfile testprofile = profile.getProfile("default");
//  testprofile.setAcceptUntrustedCertificates(true);
    //testprofile.setAssumeUntrustedCertificateIssuer(true);
    System.setProperty("webdriver.chrome.driver", "C:\\Selenium Web Driver 3.0.1\\geckodriver-v0.12.0-win32\\geckodriver.exe");
     WebDriver driver = new ChromeDriver();
     String baseURL = "<URL>";
    driver.get(baseURL);
    driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
    }

  @Test
  public void OpenBrowser() {
      driver.findElement(By.linkText("Log In")).click();
        driver.switchTo().frame(0);
        driver.findElement(By.id("tx_username")).sendKeys("my email id");


  }


  }

Error

FAILED: OpenBrowser

java.lang.NullPointerException
    at testngpackg.ARXNewTest.OpenBrowser(ARXNewTest.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:100)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)

I am getting null point error while execution of above code

Guy
  • 46,488
  • 10
  • 44
  • 88
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Guy May 11 '17 at 08:52

2 Answers2

1

You find a NullPointerException cause driver is not getting initialized before that point. You tried to initialize a WebDriver instance inside beforeMethod() method, but it was local.

Use following code :

driver = new ChromeDriver();

Instead

WebDriver driver = new ChromeDriver();

Hope it will help you.

RNS
  • 625
  • 5
  • 16
0

Here is the solution to your Question-

A few words about the Solution:

  1. Avoid creating unnecessary instances. You have ChromeDriver driver; but not used it anywhere.
  2. Keep your methods & code-blocks separated so that you can identify them properly.
  3. Remove the unwanted code when you are asking for help //ProfilesIni profile = new ProfilesIni();
  4. In System.setProperty you have mentioned the Key as webdriver.chrome.driver but you have provided the value as C:\\Selenium Web Driver 3.0.1\\geckodriver-v0.12.0-win32\\geckodriver.exe
  5. Keep you code properly formatted so that it is easier to understand code-blocks.
  6. Manage the opening & closing of { & } so you don't run into unexpected results.
  7. Provide names to the methods as per the functiosn achieved through the code within. Your OpenBrowser() method have nothing to do with Browser opening.
  8. Here is the working set of your own code with some minimal tweaks:

    public class Q43910679_null_pointer 
    {
    
        @BeforeMethod
        public void set()   
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            String baseURL = "https://gmail.com";
            driver.get(baseURL);
            driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        }
    
        @Test
        public void OpenBrowser() 
        {
            System.out.println("Open Browser Method");
        }
    
    }
    

Let me know if this Answers your Question.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352