Object reference not set to an instance of an object
-
I am using the article Inserting XML formatted data into SQL Server 2000 By faisal abdul aziz as a template for some work I doing. I have create a class, in a separate class file, to cover the 'child' elements. I have called this Answers.cs. The begining of the file looks like this: using System; using System.Xml.Serialization; namespace SGC.Apps.Consultations { public class Answers { public Answers() { } private int intQNo; private int intAns; [XmlAttribute] public int Question_No { get { return this.intQNo; } set { intQNo = value; } etc..... In web form I have button_click event routine private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { XmlSerializer serlizer = new XmlSerializer(typeof(Replies)); int intCount = 2; Answers[] ans = new Answers[intCount]; ans[1].Question_No = 3; etc...... I get the error Object reference not set to an instance of an object The highlighted line is 'ans[1].Question_No = 3; Full error is [NullReferenceException: Object reference not set to an instance of an object] Can anyone help with what stupid thing I am doing wrong? cheers Robert T Turner South Gloucestershire Council
-
I am using the article Inserting XML formatted data into SQL Server 2000 By faisal abdul aziz as a template for some work I doing. I have create a class, in a separate class file, to cover the 'child' elements. I have called this Answers.cs. The begining of the file looks like this: using System; using System.Xml.Serialization; namespace SGC.Apps.Consultations { public class Answers { public Answers() { } private int intQNo; private int intAns; [XmlAttribute] public int Question_No { get { return this.intQNo; } set { intQNo = value; } etc..... In web form I have button_click event routine private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { XmlSerializer serlizer = new XmlSerializer(typeof(Replies)); int intCount = 2; Answers[] ans = new Answers[intCount]; ans[1].Question_No = 3; etc...... I get the error Object reference not set to an instance of an object The highlighted line is 'ans[1].Question_No = 3; Full error is [NullReferenceException: Object reference not set to an instance of an object] Can anyone help with what stupid thing I am doing wrong? cheers Robert T Turner South Gloucestershire Council
1: int intCount = 2;
2: Answers[] ans = new Answers[intCount];
3: ans[1].Question_No = 3;I've numbered the lines above. In line 2 all you are doing is reserving the space for the objects. You still have to create them. So after line 2 you need to do
ans[0] = new Answer();
Also, remeber that indexes in C# are zero-based. I don't know if you knew that, but it looked odd (line 3) that you were starting on index 1.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
-
1: int intCount = 2;
2: Answers[] ans = new Answers[intCount];
3: ans[1].Question_No = 3;I've numbered the lines above. In line 2 all you are doing is reserving the space for the objects. You still have to create them. So after line 2 you need to do
ans[0] = new Answer();
Also, remeber that indexes in C# are zero-based. I don't know if you knew that, but it looked odd (line 3) that you were starting on index 1.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
Colin Angus Mackay wrote: Also, remeber that indexes in C# are zero-based. To be complete, indexes in .NET are all zero-based. [EDIT] Before the ignorant start flaming me for "indexes" check your dictionary. "Indexes" is a perfectly viable alternative to "indices". It's happened before. [/EDIT]
Microsoft MVP, Visual C# My Articles
-
I am using the article Inserting XML formatted data into SQL Server 2000 By faisal abdul aziz as a template for some work I doing. I have create a class, in a separate class file, to cover the 'child' elements. I have called this Answers.cs. The begining of the file looks like this: using System; using System.Xml.Serialization; namespace SGC.Apps.Consultations { public class Answers { public Answers() { } private int intQNo; private int intAns; [XmlAttribute] public int Question_No { get { return this.intQNo; } set { intQNo = value; } etc..... In web form I have button_click event routine private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { XmlSerializer serlizer = new XmlSerializer(typeof(Replies)); int intCount = 2; Answers[] ans = new Answers[intCount]; ans[1].Question_No = 3; etc...... I get the error Object reference not set to an instance of an object The highlighted line is 'ans[1].Question_No = 3; Full error is [NullReferenceException: Object reference not set to an instance of an object] Can anyone help with what stupid thing I am doing wrong? cheers Robert T Turner South Gloucestershire Council
= to an instance of the answer class before you can do anything with it. Because it has not been instantiated yet you get your Null Reference Exception. Try initializing each element in the array to a blank Answer class. This sounds right to me but without actually trying it i don;t know. Let me know how you get on, Cheers Kev Pearman MCP -
= to an instance of the answer class before you can do anything with it. Because it has not been instantiated yet you get your Null Reference Exception. Try initializing each element in the array to a blank Answer class. This sounds right to me but without actually trying it i don;t know. Let me know how you get on, Cheers Kev Pearman MCP
Thanks to you and all who replied Robert T Turner South Gloucestershire Council