I am attempting to return an array from a loop within a class however when doing so I am getting the error "NullReferenceException was Unhandled"
I can return a string without any issues however returning an array brings this error.
Here is my code:
class OleDB
{
string[] test;
public string [] readexcel()
{
//Below runs the OleDB connections which essentially turns excel into an Access database.
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/nlongmore/Documents/Ebay Click and Collect/Ebay MIP/MIP - UK - General Metadata.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;\"";
//Parameters for OleDB
using (OleDbConnection connection = new OleDbConnection(conn))
{ //Opens the connection
connection.Open();
OleDbCommand command = new OleDbCommand("select Value from [Domestic Shipping Service$]", connection); //Searches the column Value from the Worksheet DSS
using (OleDbDataReader dr = command.ExecuteReader())
{ //Reads in the data
for (int x = 0; x < 23; x++)
{
while (dr.Read())
{
var details = Convert.ToString(dr[0]);
test[x] = details;
}
}
connection.Close();
}
}
return test;
}
}
Any advise would be excellent. I'm still learning a lot about C# and so may have missed something completely obvious. Apologies if this is the case.