C# Object reference not set to an instance of an object

I am new C# and just need a little help with understanding how to configure this statement "obj.valueCondition[0].attributeURL".  RIght now I keep getting "Object reference not set to an instance of an object".  Can you please show me how to populate a value for this array?

MyRequestType obj = new MyRequestType();

obj.valueCondition[0].attributeURL = "test";                   ---> Help!!!

Here is how I have it defined:

valueCondition[] valueConditionField;

public partial class MyRequestType : object, System.ComponentModel.INotifyPropertyChanged {
       
        private accessContext accessContextField;
       
        private valueCondition[] valueConditionField;
       

public partial class valueCondition : object, System.ComponentModel.INotifyPropertyChanged {
       
        private string attributeURLField;
       
        private string attributeValueField;
       
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=0)]
        public string attributeURL {
            get {
                return this.attributeURLField;
            }
            set {
                this.attributeURLField = value;
                this.RaisePropertyChanged("attributeURL");
            }
        }
tmajor99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
Your array is not initialized ( obj.valueCondition = null)
Solution: Add a constructor to your MyRequestType  class initializing the array, like:
valueConditionField = new valueCondition[10];

Open in new window

where 10 is you initial size.
But if you do not know the size in advance or could change depending on your needs use a generic list as shown below:
public partial class MyRequestType : object, System.ComponentModel.INotifyPropertyChanged {
  private accessContext accessContextField;
  internal List<valueCondition> valueConditionField = new List<valueCondition>();

Open in new window

then you could modify your previous code:
MyRequestType obj = new MyRequestType();
valueCondition newVCEntry = new valueCondition()
newVCEntry.attributeURL = "test";
obj.valueConditionField.Add(newVCEntry) 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JAaron AndersonProgramming Architect @ Widener UniversityCommented:
I know in my journey Ive had to work on understanding the meanings of errors in order to trouble shoot them
Lets read this return statement with fresh thought":
C# Object reference not set to an instance of an object "
if you read this backward you can "tag" out elements to trace what is problematic; almost like problem inversion in math...
...
[object]  an of [instance] an to [set] not [reference]ing [Object]
does that make sense?
...
so you need to first define what an object is/"can be" in order to (second and then only afterward next) be able to (set) an object reference of such an object to any instances of operations -- what I mean is -- since you must first define how to construct something to only then be able to instantiate such a creation

Such exercise in deduction may help shed light for you as efforts have for me when it comes to how to determine the default constructor was not defined.

hth? hope i didnt confuse ya more...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.