0

I have the following section of code that acts on values in <Test></Test> nodes from an xml file:

var tests = new ServicesTests();
var testcase = new TestData();

var x = 0;
foreach (XPathNavigator test in Service.Select("Testname"))
{
    testcase.Testname[x] = test.Value;
    x++;
}
tests.ServiceTests.Add(testcase);

The objects were declared here:

public class ServicesTests
{
    public List<TestData> ServiceTests = new List<TestData>();
}

public class TestData
{
    ...
    public string[] Testname { get; set; }
}

Now I receive a null value reference exception when trying to set the array. I understand why, but I'm not sure what the proper way to initialize it is, since I will have no way of knowing just how many values there can be. All the examples I've read seem to assume knowing this..

Any suggestions on how this should be done properly?

Thanks

EDIT: I updated to add some code previously missing since using a list still returns a null reference error.

Jason
  • 463
  • 1
  • 11
  • 25

5 Answers5

4

If you need an expandable array you'll need to write code to manage its size and grow it as needed, or else you could use a List<string> instead and convert it to an array when you're done adding items using ToArray()

Dan
  • 1,489
  • 12
  • 16
2

Example of the above good answers.

List<string> list = new List<string>();
foreach (XPathNavigator test in Service.Select("Testname"))
{
    list.Add(test.Value);
}
var arrayIfYouStillNeedOne = list.ToArray();
Jon
  • 969
  • 4
  • 9
1

You probably want to use a generic List<string> instead. You can just use its Add method to put things into it. If you need an array when all is said and done, it's easy to convert with myList.ToArray().

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
  • Actually, you don't need linq for that, because List has its own ToArray() instance method. – phoog Apr 07 '11 at 22:37
  • I just did that, switching my declaration to use "public List Testname { get; set; } and then changing my code to be: testcase.Testname.Add(test.Value) and it still returns a null reference for testcase.Testname. Not sure what the problem is now. – Jason Apr 07 '11 at 22:40
  • Oh, ok, I have gotten so used to linq on Ienumerables I completely forgot that is a List method too! – Jamie Treworgy Apr 07 '11 at 22:42
  • You need to make a `new` list. Every object starts out null. e.g. `public List TestName = new List()` or if you want to use a property, then you need to flesh it out to lazy load, or initialize it elsewhere. – Jamie Treworgy Apr 07 '11 at 22:43
0

Use a List collection, which can dynamically resize as you add values, then you can convert the List to an array if you need to.

e.g.

var list = new List<string>();
foreach (XPathNavigator test in Service.Select("Testname"))
{
    list.Add(test.Value);
}

// If you need to get the values as an array

string[] array = list.ToArray();
Winger
  • 676
  • 3
  • 7
0

You can do this in one line with LINQ:

testcase.TestName = Service.Select("Testname").Select(x => x.Value).ToArray();

You don't even need to know the size of the array either.

Tejs
  • 40,736
  • 10
  • 68
  • 86