title: java.lang.NullPointerException in Selenium TestNG
body: <p>I'm a one-week-newbie in learning both Selenium WebDriver and Java.</p>
  
  <p>I tried to create a test script to run using TestNG.</p>
  
  <p>I have 2 java files and 1 testng.xml as below:</p>
  
  <p>[1] adminBase.java</p>
  
  <pre><code>package automation;
  
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.support.CacheLookup;
  import org.openqa.selenium.support.FindBy;
  
  import jxl.Sheet;
  import jxl.Workbook;
  import jxl.read.biff.BiffException;
  
  public class adminBase {
  static WebDriver driver;
  public static String appURL = "https://00.0.00.00:0000/admin/";
  public static String testData = "C:\src\TestData.xls";
  
  //Declare fields on pageObject that are webElements
  @FindBy(id = "j_username")
  static WebElement username;
  @CacheLookup
  
  @FindBy(id = "j_password")
  static WebElement password;
  @CacheLookup
  
  @FindBy(id = "login")
  static WebElement loginBtn;
  
  @FindBy(xpath = ".//*[@id='LogoutForm']/li/span/a")
  static WebElement logoutBtn;
  
  
  //This method will take two arguments (Username and Password)
  public void loginInfo(String sUsername, String sPassword){
      username.sendKeys(sUsername);
      password.sendKeys(sPassword);
      loginBtn.click();
      }
  
  //To read Excel file
  public String[][] getExcelData(String fileName, String sheetName) {
      String[][] arrayExcelData = null;
      try {
          FileInputStream fs = new FileInputStream(fileName);
          Workbook wb = Workbook.getWorkbook(fs);
          Sheet sh = wb.getSheet(sheetName);
  
          int totalNoOfCols = sh.getColumns();
          int totalNoOfRows = sh.getRows();
  
          arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];
          for (int i=1; i&lt;totalNoOfRows; i++){
              for (int j=0; j&lt;totalNoOfCols; j++){
                  arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
              }
          }
      }
      catch (FileNotFoundException e){
          e.printStackTrace();
      }
      catch (IOException e){
          e.printStackTrace();
      }
      catch (BiffException e){
          e.printStackTrace();
      }
      return arrayExcelData;
  }
  }
  </code></pre>
  
  <p>and also [2] adminLogin.java</p>
  
  <pre><code>package automation;
  
  import org.testng.annotations.Test;
  import org.testng.annotations.BeforeMethod;
  
  import java.util.concurrent.TimeUnit;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.testng.annotations.AfterMethod;
  import org.testng.annotations.DataProvider;
  
  public class adminLogin {
  WebDriver driver;
  adminBase adminbase;
  String testData;
  
  @BeforeMethod
  public void beforeTest() throws Exception {
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      //adminbase = PageFactory.initElements(driver, adminBase.class);
  }
  
  @DataProvider(name="loginInfo")
  public Object[][] getData() throws Exception {
      Object[][] user = adminbase.getExcelData(testData, "adminLogin");
      return user;
  }
  
  @Test(dataProvider="loginInfo")
  public void test(String appURL, String sUsername, String sPassword)
  throws Exception {
      driver.get(appURL);
      adminbase.loginInfo(sUsername, sPassword);
  }
  
  @AfterMethod
  public void afterTest() throws InterruptedException{
      Thread.sleep(3000);
      driver.quit();
  }
  }
  </code></pre>
  
  <p>[3] testng.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="Suite"&gt;
    &lt;test name="Test"&gt;
    &lt;classes&gt;
      &lt;class name = "automation.adminLogin"/&gt;
    &lt;/classes&gt;
    &lt;/test&gt; &lt;!-- Test --&gt;
  &lt;/suite&gt; &lt;!-- Suite --&gt;
  </code></pre>
  
  <p>however, i got below exception </p>
  
  <pre><code>java.lang.RuntimeException: java.lang.NullPointerException
  at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161)
  at org.testng.internal.Parameters.handleParameters(Parameters.java:429)
  at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
  at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
  at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
  at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
  at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
  at org.testng.TestRunner.privateRun(TestRunner.java:767)
  at org.testng.TestRunner.run(TestRunner.java:617)
  at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
  at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
  at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
  at org.testng.SuiteRunner.run(SuiteRunner.java:240)
  at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
  at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
  at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
  at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
  at org.testng.TestNG.run(TestNG.java:1057)
  at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
  at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
  at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
  Caused by: java.lang.NullPointerException
      at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
      at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
      at automation.adminBase.getExcelData(adminBase.java:48)
      at automation.adminLogin.getData(adminLogin.java:27)
      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:84)
      at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:135)
      ... 20 more
  </code></pre>
  
  <p>from what I understand there's error at line <strong>automation.adminLogin.getData(adminLogin.java:27)</strong></p>
  
  <p>line 27 in adminLogin.java file is this line:</p>
  
  <pre><code>(Object[][] user = adminbase.getExcelData(testData, "adminLogin");
  </code></pre>
  
  <p>I understand that the error might be due to I'm passing parameter with null. But i just don't know how to fix because i don't get any error in my files</p>
  
  <p>My code really a mess. Because I just mix and match. 
  I'm really sorry who get headache reading my code. >.&lt;</p>
  
  <p>Kindly comment and help for me to improve.
  Thanks! </p>