Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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) –  Gustavo Maciel Jan 19 '12 at 11:07
1  
And by the way... where's 'device' defined? I think its null. –  Gustavo Maciel Jan 19 '12 at 11:09
1  
Have you tried to just : MemoryStream stream = OpenFile("Content/filename.xml", OpenMode.Read); ? Then reading it as a xml is a trivial task. –  Gustavo Maciel Jan 19 '12 at 11:43
2  
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++ –  Gustavo Maciel Jan 19 '12 at 11:51
1  
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? –  Gustavo Maciel Jan 19 '12 at 12:08
show 5 more comments

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
1  
Shor answer for what i've trying to explain in the comments haha, thanks :D –  Gustavo Maciel Jan 19 '12 at 12:12
    
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? –  NDraskovic Jan 20 '12 at 10:52
    
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. –  George Duckett Jan 20 '12 at 11:18
    
Well, this program will run only on PC. What is the alternative to OpenFile dialog? –  NDraskovic Jan 23 '12 at 8:36
    
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. –  George Duckett Jan 23 '12 at 9:09
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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