简体   繁体   中英

Java: how to pass and use this array in another class

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>> array = new ArrayList<ArrayList<String>>();
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            ArrayList<String> list = new ArrayList<String>();
            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>> 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.

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>> array = new ArrayList<ArrayList<String>>();
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        ArrayList<String> list = new ArrayList<String>();
        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();
    }
  }

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM