I have a class with a method to connect to DB and fetch all users' username and password. I pass that method to TestNG @DataProvider , so @Test can use it. 

The summary looks like this

```js
public class testing2 {

  @Test(dataProvider = "dp")
  public void f(String userName, String password) {
    System.out.println(userName);
    System.out.println(password);
  }

  //Option 1 : Won't work
  //Will return [Utils] [ERROR] [Error] java.lang.NullPointerException
  @DataProvider
  public Object[][] dp() throws ClassNotFoundException, SQLException {
      //Creating an instance from the class where the method is located and then calling that method credentials
      Object[][] data = instance_of_the_class.method_to_fetch_datas_from_DB();
      return data;

      //Option 2 : works fine
      @DataProvider
      public Object[][] dp() throws ClassNotFoundException, SQLException {
          //Put the logic directly here without calling any method
          //Logic ...
          //Logic ...
          //Logic ...
          //End result is Object[][] data  
          return data;
```

The detailed method looks like this

```js
public Object[][] credentials(String driverClass,
  String DBuserName, String DBpassword, String jdbcAddress,
  String company1, String company2)
throws ClassNotFoundException, SQLException {

  Class.forName(driverClass);
  Connection con = DriverManager.getConnection(jdbcAddress, DBuserName, DBpassword);
  Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

  ResultSet rs = s
    .executeQuery("select * from users where COMPANYID = " + company1 + "or COMPANYID = " + company2);

  int total = 0;
  while (rs.next() == true) {
    total++;
  }

  Object[][] data = new Object[total][2];

  rs.beforeFirst();
  int a = 0;

  while (rs.next()) {
    data[a][0] = rs.getString("USERNAME");
    data[a][1] = rs.getString("PASSWD");
    a++;
    System.out.println(rs.getString("USERNAME"));
  }

  return data;
}
```

I tested this method separately and it works fine. 

Now in my main test class, I start using this method. It looks like this 

```js
public class testing2 {

  @Test(dataProvider = "dp")
  public void f(String userName, String password) {
 System.out.println(userName);
 System.out.println(password);
  }

  @DataProvider
  public Object[][] dp() throws ClassNotFoundException, SQLException {
      //Creating an instance from the class where method credentials() is located and then calling that method credentials
   Object[][] data =searching.credentials("interbase.interclient.Driver", "DBuserName", "DBpassword", "jdbc:interbase://10.110.10.10:3050/e:/Databases/DATABASE.IB", "5204", "5201");
   return data;
```

When I run it, it throws me 

```
[Utils] [ERROR] [Error] java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at ssdPage.SSDSearchPage.credentials(SSDSearchPage.java:52)
    at ssdTest.SSDtest.getData(SSDtest.java:106)
    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.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:55)
    at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:115)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:509)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1308)
    at org.testng.internal.Invoker.createParameters(Invoker.java:1036)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1126)
    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)

```

But if I don't call the method credentials() but I just put the logic directly in @DataProvider , it works fine. The codes example looks like this 

```js
public class testing2 {

  @Test(dataProvider = "dp")
  public void f(String userName, String password) {
    System.out.println(userName);
    System.out.println(password);
  }

  @DataProvider
  public Object[][] dp() throws ClassNotFoundException, SQLException {

    Class.forName("interbase.interclient.Driver");
    Connection con = DriverManager.getConnection("jdbc:interbase://10.110.10.10:3050/e:/Databases/DATABASE.IB", "DBuserName", "DBpassword");
    Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    ResultSet rs = s
      .executeQuery("select * from users where COMPANYID = " + "5204" + "or COMPANYID = " + "5201");

    int total = 0;
    while (rs.next() == true) {
      total++;
    }

    Object[][] data = new Object[total][2];

    rs.beforeFirst();
    int a = 0;

    while (rs.next()) {
      data[a][0] = rs.getString("USERNAME");
      data[a][1] = rs.getString("PASSWD");
      a++;
      System.out.println(rs.getString("USERNAME"));
    }

    return data;
  }
}
```

Why I can't call a method that returns `Object [][]` in TestNG @DataProvider? 
Why the logic must be written directly in @DataProvider ?

Thank You.