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. A Class like TreeNode

A Class like TreeNode

Scheduled Pinned Locked Moved C#
helpdatabasedesigndockerquestion
4 Posts 3 Posters 0 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.
  • B Offline
    B Offline
    Bahadir Cambel
    wrote on last edited by
    #1

    How could I design a class like TreeNode, I have tried but I am getting a Object Reference Exception public class classA { public classA(){} public void INeed() { classB b = new classB(); b.Add(string something); } } public class classB { private string att1; private int index ; private classB[] container; public classB() { container = new classB[100];index = 0 ; } public void Add(string addNew) { container[index].att1 = addNew;//causes error index++; } } If I do so , am getting a Object reference not set to an instance of an object error in the line that I try to add something to the container. Could you explain why ? And how could I deal with the issue ? -- modified at 15:24 Saturday 10th December, 2005

    S 1 Reply Last reply
    0
    • B Bahadir Cambel

      How could I design a class like TreeNode, I have tried but I am getting a Object Reference Exception public class classA { public classA(){} public void INeed() { classB b = new classB(); b.Add(string something); } } public class classB { private string att1; private int index ; private classB[] container; public classB() { container = new classB[100];index = 0 ; } public void Add(string addNew) { container[index].att1 = addNew;//causes error index++; } } If I do so , am getting a Object reference not set to an instance of an object error in the line that I try to add something to the container. Could you explain why ? And how could I deal with the issue ? -- modified at 15:24 Saturday 10th December, 2005

      S Offline
      S Offline
      Sean Michael Murphy
      wrote on last edited by
      #2

      Bahadir Cambel wrote:

      If I do so , am getting a Object reference not set to an instance of an object error in the line that I try to add something to the container. Could you explain why ?

      Bahadir Cambel wrote:

      container = new classB[100];

      That new just allocates the space for the array elements. It doesn't instantiate the 100 objects; you have to do that yourself.

      public void Add(string addNew) {
         container[index] = new classB(); // my addition.
         container[index].att1 = addNew;//causes error
         index++;
      }

      If you don't actually want exactly 100 elements (did you just pick an arbitrarily large number...?) you should use a dynamically sized container, like an ArrayList, rather than an array. Just my 2 cents... Share and enjoy. Sean

      B 1 Reply Last reply
      0
      • S Sean Michael Murphy

        Bahadir Cambel wrote:

        If I do so , am getting a Object reference not set to an instance of an object error in the line that I try to add something to the container. Could you explain why ?

        Bahadir Cambel wrote:

        container = new classB[100];

        That new just allocates the space for the array elements. It doesn't instantiate the 100 objects; you have to do that yourself.

        public void Add(string addNew) {
           container[index] = new classB(); // my addition.
           container[index].att1 = addNew;//causes error
           index++;
        }

        If you don't actually want exactly 100 elements (did you just pick an arbitrarily large number...?) you should use a dynamically sized container, like an ArrayList, rather than an array. Just my 2 cents... Share and enjoy. Sean

        B Offline
        B Offline
        Bahadir Cambel
        wrote on last edited by
        #3

        Yes it was a arbitrarily large number. Thanks for you replay , it solved my problem. Bado

        I 1 Reply Last reply
        0
        • B Bahadir Cambel

          Yes it was a arbitrarily large number. Thanks for you replay , it solved my problem. Bado

          I Offline
          I Offline
          Ista
          wrote on last edited by
          #4

          why not just use the add method to add your object. Try extending the IList Interface. It will help you class MyClass : IList I'm not an expert yet, but I play one at work. Yeah and here too.

          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