0

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.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 2
    You need to initialize the array first via test = new string[2]; or test = new String[]{"string1","string2"}; – ASA Feb 13 '14 at 09:37
  • @thefiloe that edit should have been rejected as "too minor". Please try to improve more than three spaces. – CodeCaster Feb 13 '14 at 09:40

4 Answers4

3

You don't create an instance. You are just declaring test; You would need to create an instance:

string[] test = new string[sizeOfTheArray];

However you don't need to declare test as a private field.

And just by the way: Your code does not make any sense. You are overwriting test[x] as many times as dr.Read() returns true.

for (int x = 0; x < 23; x++)
{

    while (dr.Read())
    {
       var details = Convert.ToString(dr[0]);
       test[x] = details;   
    }
}

If the while loop runs about 10 times, 9 values are lost. You are just storing the last value.

If you don't expect that behaviour, I would recommend you to use a List<string>.

class OleDB   
{
    public string [] readexcel()
    {

        List<string> test = new List<string>();
        //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.Add(details); 
                    }
                }
                connection.Close();       
            }
        }
        return test.ToArray();
    }
}
Florian
  • 5,918
  • 3
  • 47
  • 86
1

You aren't initializing the array anywhere, it has its default value NULL.

string[] test = new string[10];

would prepare your array to contain 10 elements. However, this is inflexible. Better use something that is mutable, like List.

using System.Collections.Generic;

List<string> test = new List<string>();

...

test.Add(details);

...

return test.ToArray();

By the way: your original code does a loop and then loops the data reader. Hence you will overwrite the content of test[x] multiple times. I suspect this was not intended.

Krumelur
  • 32,180
  • 27
  • 124
  • 263
1

You need 'new' when declaring an array

string[] test = new string[24];
Dumisani
  • 2,988
  • 1
  • 29
  • 40
1

You declared test as a member variable, but you didn't initialize it, so it's value is the default: null.

In any case you don't want to return a member variable, but rather define it as local variable.

class OleDB   
{

    public string [] readexcel()
    {
        string[] test = new string[23];
        //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;
    }
}
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95