0

I have the following piece of code that is suppose to extract some info from a file.

private string[][] users;
private string userID;

public void getInfo()
{

    string[] lines = System.IO.File.ReadAllLines(@"U:\Final Projects\Bank\ATM\db.txt");

    for (int i = 0; i < lines.Count(); i++ )
    {
        string[] values = lines[i].Split(',');
        for (int b = 0; b < 5; b++ )
        {

            users[i][b] = values[b];

        }


    }
}

the line users[i][b] = values[b]; returns the error : " Object reference not set to an instance of an object. " but I am not sure why. the code is suppose to read each line and split the line by , and create 2 dimensional array from the info.

Ahoura Ghotbi
  • 2,776
  • 12
  • 34
  • 61

3 Answers3

2

I think you need to allocate space for the array

string[,] users = new string[M,N];
parapura rajkumar
  • 22,951
  • 1
  • 49
  • 82
0

Unless there's code you've not shown us, you never actually created the array. Therefore, users will be null, so attempt to dereference it doesn't make sense. Hence, the exception.

Kent Boogaart
  • 165,446
  • 34
  • 376
  • 376
0

You need to allocate users:

string[][] users = new string[n][];
for(int i = 0; i < n; i++)
{
    users[i] = new string[m];
}

n and m can be variables.

Tudor
  • 58,972
  • 12
  • 89
  • 138