Catea
Catea

Reputation: 11

Put data from csv file automation testing

Hi I want to put data from my CSV file used automation testing but I have error:Attempt to read from null array My code:

public class TestData {
    String DataCSVPath = "src/androidTest/java/md/app/TestData/Data.csv";
    protected CSVReader csvReader;
    String csvCell[];

    public String readFile() throws IOException, CsvValidationException {
        csvReader = new CSVReader(new FileReader(DataCSVPath));
        while ((csvCell = csvReader.readNext()) != null) {
            String Phone = csvCell[0];
            String IDNP = csvCell[1];
            String cardNumber = csvCell[2];

        }
        return null;
    }
}

My CSV file: Phone,IDNP, card number

"064511418","59495955","9529525852" And my automation step onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));

In this line onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0])); I have error ttempt to read from null array

Hi I want to put data from my CSV file used automation testing but I have error:Attempt to read from null array My code:

public class TestData {
    String DataCSVPath = "src/androidTest/java/md/app/TestData/Data.csv";
    protected CSVReader csvReader;
    String csvCell[];

    public String readFile() throws IOException, CsvValidationException {
        csvReader = new CSVReader(new FileReader(DataCSVPath));
        while ((csvCell = csvReader.readNext()) != null) {
            String Phone = csvCell[0];
            String IDNP = csvCell[1];
            String cardNumber = csvCell[2];

        }
        return null;
    }
}

My CSV file: Phone,IDNP, card number "064511418","59495955","9529525852"

And my automation step onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0]));

In this line onView(withId(R.id.et_phone_number)).perform(typeText(csvCell[0])); I have error ttempt to read from null array

Upvotes: 0

Views: 92

Answers (1)

hfontanez
hfontanez

Reputation: 6168

I believe your problem is your while loop. The loop won't stop until CSVReader#readNext() returns null in which case, the null value is set in the array variable. Later on, when you try to access this global variable, it is null.

Instead, if your CSVReader class has a hasNext() method, you should use that to control the loop iterations. When you have a next element, the code will go inside the loop. Then, it will be safe to set the array. If you do not have this method, you may want to create it. The code should look something like this:

while (csvReader.hasNext()) {
    csvCell = csvReader.readNext();
    // Why are the strings below necessary? You aren't using them.
    String Phone = csvCell[0];
    String IDNP = csvCell[1];
    String cardNumber = csvCell[2];
}

This approach guarantees the array will never be null so long it goes into the loop at least once.

If you cannot create a hasNext() method, you need to use a temporary array.

public String readFile() throws IOException, CsvValidationException {
    String[] temp;
    csvReader = new CSVReader(new FileReader(DataCSVPath));
    while ((temp = csvReader.readNext()) != null) {
        csvCell = temp;
        // Removed unnecessary strings
    }
    return null;
}

Upvotes: 1

Related Questions