Syed Raza
Syed Raza

Reputation: 331

How To Put String Value in String Array ?? Code Attached (Error: Object reference not set to an instance of an object.)

i am getting error "Object reference not set to an instance of an object." my is here,

 public class UserProfession
    {
        public UserProfession()
        {

        }
        public System.String[] Designation
        {
            get;
            set;
        }
}

then i am using it like,

 UserProfession.Designation[0] =txt_Search.Text.ToString();

Error i mentioned you hopes for your suggestions .

-Thanks

Upvotes: 0

Views: 745

Answers (3)

Joel Coehoorn
Joel Coehoorn

Reputation: 415850

When you make an assignment to an array property, like this:

UserProfession.Designation[0] =txt_Search.Text.ToString();

what you are actually doing is calling the get section for that property... not the set. This returns the object supported the property... the whole object, and not just the index. Index lookup does not happen until after the object is returned. Once you have that object, accessing an index works in the normal way.

You get this specific exception because you have the expression UserProfession.Designation that should return a reference to an array object, but because you never initialize the array there is nothing there when you then try to find reference the 0th element. At this point the framework discovers that the array (your "object reference") is "not set to an instance of an object"... which is just a fancy way of saying it's null.

In other words, you need to have an already existing array to hold the value you want to assign. That means doing something like this:

Designation = new String[10];
public String[] Designation
{
    get;
    set;
}

However, notice that we never used the set section? So you can simplify that further, like this:

Designation = new String[10];
public String[] Designation {get;private set;}

This will keep client code from completely swapping an entire array out from under your object, but otherwise will provide the full functionality of an array property. If you provide your own backing store for the array, you could even get rid of the setter entirely with no loss of functionality:

private string[] _designation = new string[10];
public string[] Designation {get {return _designation;} }

But let's add one more wrinkle: your desire to assign the to array before initializing it indicates to me that you likely don't really know how big it will be up front. If that's the case, you probably want a collection of some kind instead of an array. A generic List is a convenient and very compatible replacement for an array. That would look like this:

private List<string> _designation = new List<string>();
public List<string> Designation {get {return _designation;}}

You can still access items in that list by index, just like you would with an array. The only difference you need to worry about right now is how you add new items:

UserProfession.Designation.Add(txt_Search.Text);    

Also notice that I removed the .ToString() call. Since your .Text property is almost certainly already a string, calling the .ToString() method is just silly.

Upvotes: 1

ayhtut
ayhtut

Reputation: 394

it must initialize the value before we use because, currently, it is null. you better add the initialization code in the constructor function.

Upvotes: 0

ajp
ajp

Reputation: 1490

You will have to initialize the object, before assigning the value. The initialization should be done just once. I have initialized the array size to ten. You can have your own values here. If you want to resize dynamically, you can use ArrayList

int length = 10;
UserProfession.Designation = new System.String[length];
UserProfession.Designation[0] =txt_Search.Text.ToString();

For more information: http://msdn.microsoft.com/en-us/library/aa287601(v=vs.71).aspx

Upvotes: 1

Related Questions