Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] [XML] Object reference not set to an instance of an object

This topic is 2863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been using XML for saving and loading data for a while and I've never had any problems will object references until now.

I'm serializing the data as follows:


[Serializable]
public class Tile3DSettings
{
public Quadrangle[] Faces;
public Vector3 Position;
public int Size;
}

public Level3DSettings Write()
{
Level3DSettings settings = new Level3DSettings();

settings.TileSettings = new Tile3DSettings[tiles.Length];

for (int i = 0; i < tiles.Length; ++i)
{
if (tiles != null)
{
settings.TileSettings = tiles.Write();
}
}

return settings;
}




This produces a correct .XML file:


<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
<Asset Type="Cthonian.SaveData">
<MapSettings Null="true" />
<CameraOrthographicSettings Null="true" />
<Level3DSettings>
<TileSettings>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 0 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 0 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 0 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 5 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item>
<Height>5</Height>
<Position>2.5 5 5</Position>
<Rotation>0 -1 0 0 0 0 -1 0 1 0 0 0 0 0 0 1</Rotation>
<Texture>0</Texture>
<Width>5</Width>
</Item>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 5 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 5 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 10 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 10 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>0 10 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>5 0 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item>
<Height>5</Height>
<Position>5 2.5 5</Position>
<Rotation>0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 1</Rotation>
<Texture>0</Texture>
<Width>5</Width>
</Item>
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>5 0 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>5 0 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item>
<Height>5</Height>
<Position>5 5 2.5</Position>
<Rotation>0 -1 0 0 1 0 0 0 0 0 1 0 0 0 0 1</Rotation>
<Texture>0</Texture>
<Width>5</Width>
</Item>
</Faces>
<Position>5 5 0</Position>
<Size>5</Size>
</Item>
<Item Null="true" />
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item>
<Height>5</Height>
<Position>5 5 7.5</Position>
<Rotation>0 -1 0 0 -1 0 0 0 -0 -0 -1 0 0 0 0 1</Rotation>
<Texture>0</Texture>
<Width>5</Width>
</Item>
<Item Null="true" />
</Faces>
<Position>5 5 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>5 10 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item>
<Height>5</Height>
<Position>5 7.5 5</Position>
<Rotation>0 -0 1 0 -1 0 0 0 -0 -1 -0 0 0 0 0 1</Rotation>
<Texture>0</Texture>
<Width>5</Width>
</Item>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>5 10 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>5 10 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 0 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 0 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 0 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 5 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item>
<Height>5</Height>
<Position>7.5 5 5</Position>
<Rotation>0 -1 0 0 0 0 1 0 -1 -0 -0 0 0 0 0 1</Rotation>
<Texture>0</Texture>
<Width>5</Width>
</Item>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 5 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 5 10</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 10 0</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 10 5</Position>
<Size>5</Size>
</Item>
<Item>
<Faces>
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
<Item Null="true" />
</Faces>
<Position>10 10 10</Position>
<Size>5</Size>
</Item>
</TileSettings>
</Level3DSettings>
</Asset>
</XnaContent>




Why would the reader throw the following error message:

Object reference not set to an instance of an object

when all the .XML data has been saved correctly?


private void XMLLoad(object sender, System.EventArgs e)
{
Stream stream = null;
OpenFileDialog openFileDialog = new OpenFileDialog();

openFileDialog.CheckFileExists = true;
openFileDialog.CheckPathExists = true;
openFileDialog.InitialDirectory = "\\";
openFileDialog.Filter = "xml files (*.xml)|*.xml";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((stream = openFileDialog.OpenFile()) != null)
{
// XML READER is only used here, replace it with StreamReader if required
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;

using (XmlReader reader = XmlReader.Create(stream, settings))
{
SaveData saveData = IntermediateSerializer.Deserialize<SaveData>(reader, null);
//game.Cameras["Orthographic"].Read(saveData.CameraOrthographicSettings);
//game.Map.Read(saveData.MapSettings);
game.Level3D.Read(saveData.Level3DSettings);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}


Share this post


Link to post
Share on other sites
Advertisement
It appears as though the serializer is writing out a bunch of null face items from the array. Perhaps it's throwing when trying to read those null entries back in? As a quick test, I would replace all instances of <Item Null="true" /> in your XML with valid data and see what happens.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!