String initialization problems

L

lavu

I have a class that has an array of string intialized to an empty
string. I am using an object
of this class in another file of the same project, and when I try to
access the string it gives
me an error "Object reference not set to instance of an object". While
debugging the value
of this string array element is <undefined value>.

eg.
public class A
{
string[] X = new string[1] {""); // will have more elements later
}

public class B
{

private A A1 = new A();

A1.X = "one" ; //error
}
 
J

Jon Skeet [C# MVP]

lavu said:
I have a class that has an array of string intialized to an empty
string. I am using an object
of this class in another file of the same project, and when I try to
access the string it gives
me an error "Object reference not set to instance of an object". While
debugging the value
of this string array element is <undefined value>.

Please post a short but complete program which demonstrates the
problem. I'm sure it'll be easy to debug when we can reproduce it.

See http://www.pobox.com/~skeet/csharp/complete.html

Jon
 
M

Marc Gravell

This has more than a handful of compiler-level bugs; you have protection
violation (private field), bad syntax {""), and confusion between
string-arrays and strings;

As such, the code you have posted cannot really help people debug your
issue, as we would need to make too many assumptions about what you actually
mean.

I suggest you post an example that actually compiles... then the good folk
here can probably fix it in about 5 seconds...

Marc
 
L

Larry Lard

lavu said:
I have a class that has an array of string intialized to an empty
string. I am using an object
of this class in another file of the same project, and when I try to
access the string it gives
me an error "Object reference not set to instance of an object". While
debugging the value
of this string array element is <undefined value>.

eg.
public class A
{
string[] X = new string[1] {""); // will have more elements later
}

public class B
{

private A A1 = new A();

A1.X = "one" ; //error
}

Hard to know what you mean here; the line you have marked as erroring is
attempting to assign a string to a string array member, so null
references will be the least of your worries.

Show us code that at least compiles, please.
 
L

lavu

I have been able to resolve the issue. This part of the code is shared
and somebody else had added new code recently that nulled the variables
overriding my initialization.
Thank for your time.
 

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