Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with serialization

Status
Not open for further replies.

VBdevil

Programmer
Sep 19, 2002
75
IL
I have an application in which I want to create an XML file.
My XML file looks something like this:

<?xml version='1.0'?>
<components>
<component name="A">
<a>1</a>
<b>1</b>
<c>
<c1>19200</c1>
<c2>8</c2>
<c3>None</c3>
<c4>1</c4>
</c>
</component>
<component name="B">
<a>1</a>
<b>1</b>
<c>
<c1>19200</c1>
<c2>8</c2>
<c3>None</c3>
<c4>1</c4>
</c>
</component>
</components>

I used the xsd.exe tool to create a class from the xml file. The class is called components.

In order to create the xml file, I entered all the data into an array, and then I wanted to enter the data into an object of type components (the class).

Here is the code I wrote:

public void CreateFile()
{
string fileName = "abc.xml"
string[] arr = new string[10];
//enter data into the array
WriteToFile(fileName, arr);
}

public void WriteToFile(string fileName, string[] data)
{
TextWriter writer;
writer = OpenFileForWrite(fileName);

XmlSerializer serializer = new XmlSerializer(typeof(components));
components comps = new components();

for (int i = 0; i < 10; i++)
{
comps.Items.name = data.name;
comps.Items.a = data.a;
comps.Items.b = data.b;
comps.Items.c[0].c1 = data.c1;
comps.Items.c[0].c2 = data.c2;
comps.Items.c[0].c3 = data.c3;
comps.Items.c[0].c4 = data.c4;
}
serializer.Serialize(writer, comps);
writer.close();
}

My problem is that there is a NullReferenceException at line:
comps.Items.name = data.name;
because comps.Items is null...
Why is this?
how can I fix it?
If there is need for further explanations - please ask..

 
I don't see where you fill the data array with the actual data. You create the array:

string[] arr = new string[10];

which tells C# to allocate memory for the array but does not fill it. So in actual fact, you have an array of 10 null strings. Then when you try to use them, they are null and you get your null pointer exception.

Load your array arr[0] = "value"; first.

Hope this helps
 
I did fill the data array with actual data..
I did it in the line: //enter data into the array
in the function:

public void CreateFile()
{
string fileName = "abc.xml"
string[] arr = new string[10];
//enter data into the array
WriteToFile(fileName, arr);
}

I just didn't write the actual code for it in my posting..
Anyway, the problem isn't from the data array. It's from the comps objects...

 
Put a test for null in there, and if it is, write an xsi:null to the xml. Or if handling the xsi:null is not supported at the receiving end, just write an empty string.

Chip H.



____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
My problem is that comps.Items is null, and I want it to be an array of size 8..

Let me explain the problem in more detail:
comps is an object of type class components.
For each component in the class there is an array named Items. There are 8 components in all, so Items should be an array of size 8.
But when I write the line:
components comps = new components();
It creates the comps object with an Items object, but comps.Items is null.
And then when it gets to the line:
comps.Items.name = data.name;
I get the NullReferenceException, with the detail: "Object reference not set to an instance of an object."

What I need is: comps.Items[8], where each comps.Items[0], comps.Items[1] etc is not null.

I'm guessing that comps.Items is null because I don't have memory allocated for the elements of the items array. How do I do this?

I would appreciate any help, because I have no idea as of how to do this...

Thanks
 
When initialising your object you call the constructor

Code:
 components comps = new components();

Does this constructor populate your collection?

If not then the code sample above does not call any method to populate your collection so your items will be null.

HTH

Smeat

 
The constructor does not populate the collection, and I can not change it's code, because that code was automatically generated by the xsd.exe tool.

My problem is exactly this - how do I populate the collection? How do I allocate memory for the elements of the items array?
(right now the result is that comps.items = null)

if u could write code - that would be great..
Even a direction of how to do this would be good...

Thanks
:)
 
Is there not a comps.add, or comps.items.add that you could use?


Hope this helps.
 
No there isn't an add method - already tried that...

I think I have to allocate memory for the elements of the items array using the new keyword, but I don't know how...
 
I didn't understand your question.
You wrote: "that would let you pass in the comps"
What do mean by this?

Anyway, I don't have an overload of New...

 
What I have is this class (that was auto-generated, so I can't modify it):
public class components
{
private componentsComponent[] itemsField;

public componentsComponent[] Items
{
get {return this.itemsField;}
set {this.itemsField = value;}
}
}

And when I write (in another class):
components comps = new components();
it creates an object called comps, which has an array called itemsField and an array called Items, which are both Null.
What I need is to allocate memory for these arrays.

I'll simplify it:
Let's say I have class A that looks like this:
class A
{
string[] arr1;
string[] arr2;
}
and I have class B in which I define an instace of class A:
class B
{
A instanceOfClassA = new A();
}
I get an object called instanceOfClassA that has two integer arrays: arr1 and arr2, that are both Null.
What I need to do is add code that initializes these arrays to a certain size, let's say, so they are of size 10..
Otherwise when I write arr1[4], I'll get an exception because arr1[4] is null..

How can I do this initialization (or allocation of memory)?

 
Not sure if this answers your question. This allocates memory but doesn't set values.

public class A
{
public string[] arr1;
public string[] arr2;
//This constructor will set the size of these arrays.
//The values of an any individual string is still nothing
//until it is set.
public A(int SizeOfArray)

{
arr1 = new string[SizeOfArray];
arr2 = new string[SizeOfArray];
}
}

class B
{
public A instanceOfClassA = new A(8);

}

//You can then write
B instanceOfB = new B();
instanceOfB.instanceOfClassA.arr1[1] ="sadf";



OR moving the setting of the array size to Class B....
public class A
{
public string[] arr1;
public string[] arr2;
}

class B
{
public A instanceOfClassA;
public B(int SizeOfArray)
{
instanceOfClassA = new A();
instanceOfClassA.arr1 = new string[SizeOfArray];
instanceOfClassA.arr2 = new string[SizeOfArray];

}
}

//Then you write this...
B instanceOfB = new B(8);
instanceOfB.instanceOfClassA.arr1[1] ="sadf";
 
Thanks stsuing!!!
Your reply really helped me solve my problem!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top