Title: Storing contents of a webtable in a 2d matrix
Tags: java;arrays;multidimensional-array
Question: I am trying to get the contents of a webtable using selenium and then store the contents in a 2d matrix.
Below is my code :
//Locate the webtable
WebElement reportTable = driver.findElement(By.xpath("//*[@id='pageContainer']/div/div[2]/table[2]"));
int rowCount = driver.findElements(By.xpath("//*[@id='pageContainer']/div/div[2]/table[2]/tbody/tr")).size();  //Get number of rows
System.out.println("Number of rows : " +rowCount);
String[][] reportMatrix = new String[rowCount-1][];    //Declare new 2d String array
//rowCount-1 because the first row is header which i don't need to store
int mainColCount = 0;
for(int i=2;i<=rowCount;i++)  //Start count from second row, and loop till last row
int columnCount = driver.findElements(By.xpath("//*[@id='pageContainer']/div/div[2]/table[2]/tbody/tr["+i+"]/td")).size();  //Get number of columns
System.out.println("Number of columns : " +columnCount);
mainColCount = columnCount;
for(int j=1;j<=columnCount;j++)    //Start count from first column and loop till last column
String text = driver.findElement(By.xpath("//*[@id='pageContainer']/div/div[2]/table[2]/tbody/tr["+i+"]/td["+j+"]/div")).getText();  //Get cell contents
System.out.println(i + " " + j + " " + text);
reportMatrix[i-2][j-1] = text;  //Store cell contents in 2d array, adjust index values accordingly
//Print contents of 2d matrix
for(int i=0;i<rowCount-1;i++)
for(int j=0;j<mainColCount;j++)
System.out.print(reportMatrix[i][j] + " ");
System.out.println();
This gives me a Null Pointer Exception at "reportMatrix[i-2][j-1] = text".
I don't understand what I am doing wrong. Do I have to give even the second index when I declare the 2d array ?
Thanks in advance.

Here is the accepted answer: Unless you're a student who is studying multi-dimensional arrays, or you are otherwise constrained by an API you're required to use, just avoid arrays. You'll stay saner longer :)
If you HAVE to use a 2D array, it's wise to remember that you are not actually creating a matrix. You are creating a 1D array, and each element of this array is another 1D array. When you think of it that way, it becomes clear that you definitely have to initialize the "columns" arrays as well as the "rows" array.
This line:
String[][] reportMatrix = new String[rowCount-1][];
will initialize report matrix to have rowCount - 1 rows, and null for each and every set of columns.
Inside your first loop, after you have identified the number of columns, you want to do something like so:
reportMatrix[i] = new String[columnCount];
for(int j=1;j<=columnCount;j++) ...
This will allow you to have different number of columns in each row, if necessary.
Then, in your print loop, you should use the array lengths to print out the rows and columns. Remember to subtract 1 from the length attribute, since the this represents the number of elements in the array, and we almost always use zero-indexed for loops.
//Print contents of 2d matrix
for(int i=0; i < reportMatrix.length - 1; i++)
for(int j=0; j < reportMatrix[i].length - 1; j++)
System.out.print(reportMatrix[i][j] + " ");
System.out.println();


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:03/25/16', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><arrays><multidimensional-array>', 'Title': 'Storing contents of a webtable in a 2d matrix', 'ViewCount': 1029, 'id': 36226414, 'user_AboutMe': None, 'user_AccountId': 5741947.0, 'user_DisplayName': 'sanaku', 'user_Reputation': 309.0, 'user_Views': 88.0, 'user_id': 4534385.0}}