سؤال

I have assigned two string array:

string[] SelectColumns = {},WhereColumns={};

Both of them are full of data items. For example SelectColumns.length = 7,WhereColumns.Length=3;

When i went to implement them i got an exception: object reference not set to an instance of an object. I am using them in below:

for (int i = 0; i < SelectColumns.Length; i++)
{
    DPS._SelectCol[i] = SelectColumns[i];
}

for (int i = 0; i < WhereColumns.Length; i++)
{
    DPS._WhereCol[i] = WhereColumns[i];
}

Here DPS is the object of a class, which is given below:

public class DefaultProfileSetting
{
    private string Server;

    public string _Server
    {
        get { return Server; }
        set { Server = value; }
    }

    private string Authentication;

    public string _Authentication
    {
        get { return Authentication; }
        set { Authentication = value; }
    }
    private string Login;

    public string _Login
    {
        get { return Login; }
        set { Login = value; }
    }
    private string Pass;

    public string _Pass
    {
        get { return Pass; }
        set { Pass = value; }
    }
    private string DB;

    public string _DB
    {
        get { return DB; }
        set { DB = value; }
    }
    private string Table;

    public string _Table
    {
        get { return Table; }
        set { Table = value; }
    }
    private string[] SelectCol;

    public string[] _SelectCol
    {
        get { return SelectCol; }
        set { SelectCol = value; }
    }
    private string[] WhereCol;

    public string[] _WhereCol
    {
        get { return WhereCol; }
        set { WhereCol = value; }
    }
}
هل كانت مفيدة؟

المحلول

You probably have just string array reference _SelectCol but not actual array and need to instantiate the _SelectCol string array to allocate memory to its elements.

DPS._SelectCol = new string [SelectColumns.Length];

for (int i = 0; i < SelectColumns.Length; i++)
{
    DPS._SelectCol[i] = SelectColumns[i];
}

نصائح أخرى

I don't see anywhere in DefaultProfileSetting where you initialize the fields behind _WhereCol and _SelectCol, so these are null.

At the least you should have:

private string[] SelectCol = new string[size];

Though these should have some sort of initial population or you will get IndexOutOfBoundsException as well.

Most probably your DPS array properties are not initialized with the correct length. You'd best place a break point and debug your solution, so that you can see for you self where exactly it goes wrong.

If you say that SelectColumns and WhereColumns are already filled with values, then i bet that DPS._SelectCol is causing problems.

You have to initialize that array at the right size. Something like : DPS._SelectCol = new string[SelectColumns.Length];

If you leave your arrays behind and start using List then you don't have these problems anymore.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top