Xtreme Newbie: String Arrays

R

Rob

I am an Xtreme newbie to C# and am trying to build an array of strings. I
get the following error and don't know what I'm doing wrong.

System.NullReferenceException: Object reference not set to an instance of an
object.

Here is a diliuted example.

string[] strArray;
string strValue;
int intIndex = 0;
str_value = "Test";

for( intIndex = 1; intIndex < 10; intIndex++ )
{
strArray[intIndex] = strValue;
intIndex++;
}
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

Well, you are not initializing your array to anything. When you call
this line in your loop:

strArray[intIndex] = strValue;

You are trying to do so without initializing the array beforehand. I
imagine you are getting a NullReferenceException.

You are also populating every other element of the array (you increment
intIndex twice in each iteration of the loop).

Here is an example which should work:

// The array. Initialize to 10 elements. Remember, arrays are zero-based.
string[] strArray = new string[10];

// The value.
string strValue = "Test";

// Cycle through the array and populate each element.
for (int intIndex = 0; intIndex < 10; intIndex++)
{
// Set the value.
strArray[intIndex] = strValue;
}
 
P

Peter Duniho

Rob said:
I am an Xtreme newbie to C# and am trying to build an array of strings. I
get the following error and don't know what I'm doing wrong.

System.NullReferenceException: Object reference not set to an instance of an
object.

The error means just what it says: the object reference isn't set to an
instance of an object.

I would be surprised if the code you posted compiles, as what you've
posted clearly has uninitialized variables "strArray" _and_ "strValue",
as well as an undefined identifier "str_value".

I recommend that when posting code samples that you intend to use to
demonstrate a problem, you make sure the code is complete, and that it
compiles. In this case, because there are so many errors in the code
you posted, it's hard to suggest exactly what is wrong.

That said, usually the error you get happens when you've got a variable
that has in fact been initialized (such as a method parameter or class
member field), but to null rather than an actual value. In the code you
posted, the only place where you attempt to reference an object in a way
that would require the reference to be non-null is "strArray[intIndex]",
so I strongly suspect that whatever "strArray" actually is, it's been
initialized to "null" instead of some actual string[] instance.

Try putting something like "strArray = new string[10];" somewhere.

Pete
 
J

Jon Skeet [C# MVP]

I am an Xtreme newbie to C# and am trying to build an array of strings. I
get the following error and don't know what I'm doing wrong.

You've declared an array variable, but not actually created an array.

If you have:

strArray = new string[11];

then your code won't break.
 
R

Rob

Thanks for your response. What I really need to know is how do I
initialize the array if I don't know how many elements it is going to
contain.

Nicholas Paldino said:
Rob,

Well, you are not initializing your array to anything. When you call
this line in your loop:

strArray[intIndex] = strValue;

You are trying to do so without initializing the array beforehand. I
imagine you are getting a NullReferenceException.

You are also populating every other element of the array (you increment
intIndex twice in each iteration of the loop).

Here is an example which should work:

// The array. Initialize to 10 elements. Remember, arrays are
zero-based.
string[] strArray = new string[10];

// The value.
string strValue = "Test";

// Cycle through the array and populate each element.
for (int intIndex = 0; intIndex < 10; intIndex++)
{
// Set the value.
strArray[intIndex] = strValue;
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob said:
I am an Xtreme newbie to C# and am trying to build an array of strings. I
get the following error and don't know what I'm doing wrong.

System.NullReferenceException: Object reference not set to an instance of
an object.

Here is a diliuted example.

string[] strArray;
string strValue;
int intIndex = 0;
str_value = "Test";

for( intIndex = 1; intIndex < 10; intIndex++ )
{
strArray[intIndex] = strValue;
intIndex++;
}
 
M

Morten Wennevik [C# MVP]

Thanks for your response. What I really need to know is how do I
initialize the array if I don't know how many elements it is going to
contain.

You should use the List class if you know what types the list should contain

List<string> strings = new List<string>();

You know have an empty list that can contain strings.
To add strings to this list, simply

strings.Add("Hello World");

You can then access it with

string s = strings[0]; // s will contain "Hello World"
 
M

Morten Wennevik [C# MVP]

Rob,

You can always use an List<T> instance (in this case, List<string>) and
add the elements to the array as you need to.

Nicholas, you need to tweak your system clock. You are running a tad fast ;)
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

You can always use an List<T> instance (in this case, List<string>) and
add the elements to the array as you need to.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob said:
Thanks for your response. What I really need to know is how do I
initialize the array if I don't know how many elements it is going to
contain.

Nicholas Paldino said:
Rob,

Well, you are not initializing your array to anything. When you call
this line in your loop:

strArray[intIndex] = strValue;

You are trying to do so without initializing the array beforehand. I
imagine you are getting a NullReferenceException.

You are also populating every other element of the array (you
increment intIndex twice in each iteration of the loop).

Here is an example which should work:

// The array. Initialize to 10 elements. Remember, arrays are
zero-based.
string[] strArray = new string[10];

// The value.
string strValue = "Test";

// Cycle through the array and populate each element.
for (int intIndex = 0; intIndex < 10; intIndex++)
{
// Set the value.
strArray[intIndex] = strValue;
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rob said:
I am an Xtreme newbie to C# and am trying to build an array of strings.
I get the following error and don't know what I'm doing wrong.

System.NullReferenceException: Object reference not set to an instance
of an object.

Here is a diliuted example.

string[] strArray;
string strValue;
int intIndex = 0;
str_value = "Test";

for( intIndex = 1; intIndex < 10; intIndex++ )
{
strArray[intIndex] = strValue;
intIndex++;
}
 
P

Peter Duniho

Rob said:
Thanks for your response. What I really need to know is how do I
initialize the array if I don't know how many elements it is going to
contain.

You don't. That is, at a minimum you have to have some idea of the
maximum number of items it's going to contain. That's how arrays work.
They need to be allocated before you start putting stuff into them.

If you have a design need to be able to arbitrarily add things to the
collection, you should look at .NET collection classes, such as the
non-generic ArrayList or generic List<>.

For example:

string[] strArray;
List<string> strList = new List<string>();
string strValue = "Test";

for (int intIndex = 0; intIndex < 10; intIndex++)
{
strList.Add(strValue);
}

strArray = strList.ToArray();

Note that even here, you still need to initialize the data structure
into which you're putting your strings. It just happens that in this
case, it's a data structure that can essentially expand as necessary as
you add new items.

You can usually use the ArrayList and List<> classes in exactly the same
way you'd use an array (you can index them, use "foreach" with them,
etc.), but if you run into a situation where you must have something
that is actually a specific kind of array, then the ToArray() method is
useful for that (as illustrated above).

Pete
 
G

G.Doten

Rob said:
I am an Xtreme newbie to C# and am trying to build an array of strings. I
get the following error and don't know what I'm doing wrong.

System.NullReferenceException: Object reference not set to an instance of an
object.

Here is a diliuted example.

string[] strArray;
string strValue;
int intIndex = 0;
str_value = "Test";

for( intIndex = 1; intIndex < 10; intIndex++ )
{
strArray[intIndex] = strValue;
intIndex++;
}

Try these changes:

string[] strArray = new string[10];
strValue = "Test";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top