A Class like TreeNode
-
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 -
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, 2005Bahadir 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 -
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. SeanYes it was a arbitrarily large number. Thanks for you replay , it solved my problem. Bado
-
Yes it was a arbitrarily large number. Thanks for you replay , it solved my problem. Bado