0
\$\begingroup\$

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);           
            rezultat.AsyncWaitHandle.WaitOne();

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

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

\$\endgroup\$
10
  • \$\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\$ Commented Jan 19, 2012 at 11:07
  • 1
    \$\begingroup\$ And by the way... where's 'device' defined? I think its null. \$\endgroup\$ Commented Jan 19, 2012 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\$ Commented Jan 19, 2012 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\$ Commented Jan 19, 2012 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\$ Commented Jan 19, 2012 at 12:08

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Shor answer for what i've trying to explain in the comments haha, thanks :D \$\endgroup\$ Commented Jan 19, 2012 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
    Commented Jan 20, 2012 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\$ Commented Jan 20, 2012 at 11:18
  • \$\begingroup\$ Well, this program will run only on PC. What is the alternative to OpenFile dialog? \$\endgroup\$
    – NDraskovic
    Commented Jan 23, 2012 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\$ Commented Jan 23, 2012 at 9:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .