I want to enable my program to load "savegames", and I followed the tutorial on MSDN, but when I run my code, I get the NullReferenceException error. I use an xml file as my savegame data container, and it works, but at this point I have to rebuild my game every time I make some changes to that xml file, and I want to be able to do it during run-time. The xml file is stored in my Content folder. This is the code of my load method:

   public void Otvori()

            IAsyncResult rezultat = device.BeginOpenContainer("OtvaranjeDemo", null, null);           

            container = device.EndOpenContainer(rezultat);
            if (!container.FileExists(imeDatoteke))
            Stream stream = container.OpenFile(imeDatoteke, FileMode.Open);
            XmlSerializer serializer = new XmlSerializer(typeof(PostavkeStanice));
            PostavkeStanice postavke = (PostavkeStanice)serializer.Deserialize(stream);

The compiler throws me the error at this line "IAsyncResult rezultat = device.BeginOpenContainer("OtvaranjeDemo", null, null);"

Since this is a method in a game library, I thought it had something to do with variable definition scope, but I declared the variable in the game wide scope, and I use this method inside the Update method, so that should not be the problem? Do you have any ideas? Thanks

  • \$\begingroup\$ Why dont you use simple file handlers? Is a lot easier than the async ones, and im not sure if you want async here, because, in anyway you cant start the game before the gamestate is loaded(well, i think) \$\endgroup\$ – Gustavo Maciel Jan 19 '12 at 11:07
  • 1
    \$\begingroup\$ And by the way... where's 'device' defined? I think its null. \$\endgroup\$ – Gustavo Maciel Jan 19 '12 at 11:09
  • 1
    \$\begingroup\$ Have you tried to just : MemoryStream stream = OpenFile("Content/filename.xml", OpenMode.Read); ? Then reading it as a xml is a trivial task. \$\endgroup\$ – Gustavo Maciel Jan 19 '12 at 11:43
  • 2
    \$\begingroup\$ Oh and take a look, every object in C# start as null! Until you use some method to fill it, or make a variable = new type(); it would still be null. It isnt equal C++ \$\endgroup\$ – Gustavo Maciel Jan 19 '12 at 11:51
  • 1
    \$\begingroup\$ This is because i've said 'fill with methods, OR new instance' Memory Streams should be initialized with OpenFile or with other Stream allocation Method. But also, i'm pretty sure that there is a MemoryStream(stream) constructor... are you doing it right? \$\endgroup\$ – Gustavo Maciel Jan 19 '12 at 12:08

You need to get a device first using StorageDevice.BeginShowSelector In the Async callback of that method, you can assign the device to your variable. Only then can you call methods on it.
See Getting a StorageDevice Asyncronously (MSDN).

I suggest using the EasyStorage helper classes that encapsulates a lot of the setup of loading/saving.

  • 1
    \$\begingroup\$ Shor answer for what i've trying to explain in the comments haha, thanks :D \$\endgroup\$ – Gustavo Maciel Jan 19 '12 at 12:12
  • \$\begingroup\$ Can the methods used in windows forms be used to solve this problem, or does it conflict with some XNA restrictions. Also, can I show an OpenFile dialog in the game and how? \$\endgroup\$ – NDraskovic Jan 20 '12 at 10:52
  • \$\begingroup\$ Whether there are restrictions depends on your target platform (PC/xbox). Given that games run in a continual loop, opening the openfile dialog would need some fancy coding to get it to work. XNA (and games in general) work differently to an event driven system like winforms. \$\endgroup\$ – George Duckett Jan 20 '12 at 11:18
  • \$\begingroup\$ Well, this program will run only on PC. What is the alternative to OpenFile dialog? \$\endgroup\$ – NDraskovic Jan 23 '12 at 8:36
  • \$\begingroup\$ Thinking about it, you might be able to get the OpenFileDialog to work, altough the game window might go a bit weird. Add a reference to the System.Windows.Forms assembly and namespace. \$\endgroup\$ – George Duckett Jan 23 '12 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.