Java - Create Object[][] Dynamically

Associate
Joined
27 Jul 2009
Posts
590
Location
UK
Another Java question, sorry guys. I am trying to do something similar to the below dynamically.

Code:
String[] field1 = { "Liam", "23", "M" };
String[] field2 = { "Sarah", "32", "F" };
String[] field3 = { "Thomas", "45", "M" };
Object[][] retObjArr = { { field1 }, { field2 }, { field3 } };
[/quote]The Object[][] is passed to a method that uses the elements in the String[]'s as test data.
 
[quote] @Test(dataProvider = "DP1")
void dpTest(String[] fields) {
System.out.println("Firstname: " + fields[0]);
}
The above code works fine, however, it's not the solution I'm looking for. Ideally I would like to be able to be able to load the Object[][] array dynamically depending on how many results are contained in ArrayList which is returned by my database query method.

I've been trying variations of the code below casting to and from String[] and Object[] and using ArrayList.toArray(), in the vain hope something will work.

Code:
 ArrayList<String[]> listRows = null;
try {
listRows = myDb.getRows("SELECT * FROM Quotes;");
assert listRows.size() > 0;
} catch (Exception e) {
System.err.println("No results returned.");
}
 
Object[][] Rows = new Object
[listRows.size()][];
 
for (int i = 0; i < listRows.size(); i++) {
String[] tempRow = listRows.get(i);
Rows[i] = tempRow;
}
 
return (rows)
When the Object[][] from the above is passed to dpTest(String[] Quotes) I get an ArrayIndexOutBounds 1 (the number 1 corresponds to the number of arguments in the method declaration).

I take it I'm not passing the information correctly, though, when I output using Arrays.toString(array) and Arrays.deepToString(array) the contents are the same or similar.
 
Last edited:
I don't understand what the 2D Object[][] array is being used for, do you have some other code that makes use of it?
 
Last edited:
Why not use something like an ArrayList? I must be missing somethihng :o
I am :). My method that retrieves rows from the db returns an ArrayList, however, TestNG's DataProvider annotation requires a method that returns Object[][].

I don't understand what the 2D Object[][] array is being used for, do you have some other code that makes use of it?
It's used by TestNG to create test cases, the method annotated as DataProvider should provide an Object[][] to the @Test method, from what I gather the size of the first dimension is the number of cases to run and the second dimension contains the parameters as elements, in my case Strings. It works wonderfully if I go the Object[][] rows = { { "Liam", "Davis" , "23" }, { "Tim", "Jones", "33" }} route but it's not very pratical when you have 36 fields you need to pass.

I''m not very proficient in Java, the last time I used it was around 3 years ago, so I believe it's my butchering of the lanaguage that is causing the issue.

I am hoping someone here will be able to say "You're doing it all wrong, you should be creating or casting to the Object[][] this way..."
 
After looking at TestNG I think I now understand what your are trying to achieve. So you want your test data to come from the database? I would have thought the whole idea would be to provide quick test data without relying on databases etc.

However looking at the code above, where did you put the output using Arrays.toString(array) and Arrays.deepToString(array) ? Did you just ensure you are getting data back form the db? It's a bit difficult to tell whether you have made a mistake somewhere as you don't provide the full syntax of your data provider method so did you make a mistake with the name or annotation?

Something strange in your code snippet as well you declare "Object[][] Rows = new Object[listRows.size()][];" but then return "rows".

Java is case sensitive so Rows != rows.
 
After looking at TestNG I think I now understand what your are trying to achieve. So you want your test data to come from the database? I would have thought the whole idea would be to provide quick test data without relying on databases etc.
Apologies for not explaining earlier, we're going to be using the scripts to test an engine that generates data using a large amount of information. Some of the information should have very specific effects on the result generated and therefore we are using previous records to confirm that recent changes have affected the way this data is processed. Also, many rating factors should not have been affected by changes so we need to ensure that results generated now match those generated by the previous release.

Here is the code, the row typo was a result of me copying into Notepad and deleteing the Eclipse formatting.

Code:
import java.util.ArrayList;
import java.util.Arrays;

import org.testng.*;
import org.testng.annotations.*;

import com.thoughtworks.selenium.*;


@Test
public class TestBrand {
    
    Selenium selenium;
    DbHelper myDb = new DbHelper();
    ModifyFunctions mf = new ModifyFunctions();


    @DataProvider(name = "DP1")
    Object[][] getData() {
        myDb.connectDB("MS Access");
        ArrayList<String[]> listRows = null;

        try {
            listRows = myDb.getRows("SELECT * FROM BrandQuotes;");
            assert listRows.size() > 0;
        } catch (Exception e) {
            System.err.println("No results returned.");
        }

        Object[][] Rows = new Object
[listRows.size()][];

        for (int i = 0; i < listRows.size(); i++) {

            
            
            String[] tempRow = listRows.get(i);
            Rows[i] = tempRow;
        }

        System.out.println("Printing Array ######## \n");
        System.out.println(Arrays.toString(Rows));
        System.out.println("Printing  Deep Array ######## \n");
        System.out.println(Arrays.deepToString(Rows));

        /*
        Object[] testRow = null;
            
        for (int i = 0; i < Rows.length; i++) {
            testRow =  Rows[i];
            for (int n = 0; n < testRow.length; n++) {
                System.out.print(testRow[n] + "\t");
            }
            System.out.println("\n");
        }
        System.out.println(Rows[1].length);
    */
        return (Rows);
    
    }

    @Test(dataProvider = "DP1")
    void dpTest(String[] Quotes) {
        // Test steps to be performed
        System.out.println("Entered main test method");
    }


}
 
Last edited:
Back
Top Bottom