Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Object reference not set to an instance of an object

Object reference not set to an instance of an object

Scheduled Pinned Locked Moved C#
helpquestiondatabasesql-serversysadmin
5 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rturner003
    wrote on last edited by
    #1

    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

    C E 2 Replies Last reply
    0
    • R rturner003

      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

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • C Colin Angus Mackay

        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

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • R rturner003

          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

          E Offline
          E Offline
          exhaulted
          wrote on last edited by
          #4

          = 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

          R 1 Reply Last reply
          0
          • E exhaulted

            = 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

            R Offline
            R Offline
            rturner003
            wrote on last edited by
            #5

            Thanks to you and all who replied Robert T Turner South Gloucestershire Council

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups