Title: Java: how to pass and use this array in another class
Tags: java;testng
Question: I've got a CSV reader class and I've got a user creator class.
I want the user creator class to take the array, generated by the CSV reader and assign the data to some variables, but Im getting a nullpointerexception

Here is the CSV reader class:

```public class CSVData {

    private static final String FILE_PATH="C:\\250.csv";

    @Test
    public static void main() throws Exception {

        CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
        ArrayList<ArrayList<String&gt;&gt; array = new ArrayList<ArrayList<String&gt;&gt;();
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            ArrayList<String&gt; list = new ArrayList<String&gt;();
            for (int i=0;i<5;i++) { //5 is the number of sheets
               list.add(nextLine[i]);
            }
            array.add(list);

        }

        /*for(int x=0;x<array.size();x++) {
            for(int y=0;y<array.get(x).size();y++) {          
            }
        }*/
        AppTest3 instance = new AppTest3();
        instance.settingVariables(array);
        reader.close();
        }
    }
```

And here is the user creator class

```public class AppTest3 extends AppData (which extends CSVData) {

    private String[] firstname;
    private String[] lastname;

    public void settingVariables(ArrayList<ArrayList<String&gt;&gt; array) {
        int randomUser1 = randomizer (1, 250);
        int randomUser2 = randomizer (1, 250);
        String firstname1 = array.get(randomUser1).get(0);
        String firstname2 = array.get(randomUser2).get(0);
        String lastname1 = array.get(randomUser1).get(1);
        String lastname2 = array.get(randomUser2).get(1) ;
        //firstname = { firstname1, firstname2 }; //this doesnt work, dunno why
        //lastname = { lastname1, lastname2 };
        firstname[0] = firstname1.replace(" ", "");
        firstname[1] = firstname2.replace(" ", "");
        lastname[0] = lastname1.replace(" ", "");
        lastname[1] = lastname2.replace(" ", "");
    }
@Parameters({ "driver", "wait" })
@Test(dataProvider = "dataProvider")
public void oneUserTwoUser(WebDriver driver, WebDriverWait wait)
        throws Exception {
        // A user signs up, then signs out, then a second user signs up
        for (int y = 0; y < 2; y++) {
            String email = firstname[y].toLowerCase() + randomNumber + "@"
                    + lastname[y].toLowerCase() + emailSuffix;
    //test here
    }
}
```

This is the error message 

```FAILED: oneUserTwoUser(org.openqa.selenium.support.events.EventFiringWebDriver@5efed246, org.openqa.selenium.support.ui.WebDriverWait@2b9f2263)
java.lang.NullPointerException
    at com.pragmaticqa.tests.AppTest3.oneUserTwoUser(AppTest3.java:57)
    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)
```

PS:         System.out.println(firstname[0]); doesnt display anything in the console.
System.out.println(array); displays the list of arrays.

EDIT: I found out what the problem was:
first of all I changed the way I initialize string[] to this

```String[] firstname = new String[2];
String[] lastname = new String[2];
```

Now firstname[0] returns a value.

However, when I try to system.out.println firstname[0] in the next method that actually contains the test case, it returns null.

So I have to find a way to pass those strings to that method.

Comment: From where you get the index y???

Comment: 1 sheet in the csv file with 5 columns.

Comment: I removed the loop, that provides Y, I will bring it back in the code, sorry.

Comment: How many sheets in one line?

Here is the accepted answer: I've found a solution to this problem by heavily refactoring the code.
Its all explained here

Java: how can I put two String[] objects into one object and pass them to the next method?


Here is another answer: In you ```main``` method ```instance``` is just a local variable that is lost after method execution ends.: 

``` AppTest3 instance = new AppTest3();  // just local variable that is lost after method execution ends.
 instance.settingVariables(array);
```

Thus, ```oneUserTwoUser``` is invoked on another instance that has no parameters set. You can see this with debugger.

You can put initialization to method ```before``` as below into Apptest3 class:

```public class AppTest3 {

  AppTest3 instance; // field used by tests

  @BeforeSuite(alwaysRun = true) 
  public void before() throws Exception {

    CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
    ArrayList<ArrayList<String&gt;&gt; array = new ArrayList<ArrayList<String&gt;&gt;();
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        ArrayList<String&gt; list = new ArrayList<String&gt;();
        for (int i=0;i<5;i++) { //5 is the number of sheets
           list.add(nextLine[i]);
        }
        array.add(list);

    }

    instance = new AppTest3();  /// init your instance here
    instance.settingVariables(array);
    reader.close();
    }
  }
```

Comment for this answer: it is much easier now. Put a breakpoint there and see what `array` actually contains.
Comment for this answer: If they are still null, then `settingVariable` method is not executing. you can check it by calling `before` method inside `oneUserTwoUser` method
Comment for this answer: you are right. i did that and now my test starts, opening a firefox instance and it freezes. I also made that "instance" variable public, but nothing has changed.
Comment for this answer: FAILED CONFIGURATION: @BeforeSuite before
java.lang.NullPointerException
 at com.pragmaticqa.tests.AppTest3.settingVariables(AppTest3.java:32)
 at com.pragmaticqa.tests.CSVData.before(CSVData.java:36)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
Comment for this answer: firstname[0] = firstname1.replace(" ", "");
Comment for this answer: Thanks to the technique you taught me I was able to debug a little bit better and I found out that the actual test method doesnt know the value of firstname[0]. So I need to find a way to pass these to that method. I've edited my question @Tala
Comment for this answer: I have refactored my code and posted the new question http://stackoverflow.com/questions/18145005/java-how-can-i-put-two-string-objects-into-one-object-and-pass-them-to-the-ne?noredirect=1

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:08/09/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><testng>', 'Title': 'Java: how to pass and use this array in another class', 'ViewCount': 4864, 'id': 18142916, 'user_AboutMe': '<p>Java, Kotlin, Swift, some Ruby and some Haskell</p>\n<p>Clean Architecture, Design Patterns, SOLID principles, packaging principles</p>\n<p>Boyscout rule</p>\n', 'user_AccountId': 3039548.0, 'user_DisplayName': 'Kaloyan Roussev', 'user_Reputation': 14217.0, 'user_Views': 1277.0, 'user_id': 2576903.0}}