locked
Windows 8- WriteTextAsync - ocassional “Object Reference not set…” error

    Question

  • Hi - in my app, a key piece of the functionality is saving the data to file which happens when the user navigates away from a page.

    I have the following code

    string xmlData = ObjectSerializer<MyClass>.ToXml(savedData);

    if (xmlData != null)        
    {
        var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync(filename);
        if (item != null)
        {
            Task t = BackupFile(filename);
            await t;
        }

        StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
        await FileIO.WriteTextAsync(localFile, xmlData);
    }

    Very, very occasionally (about 1% of the time) I am getting an "Object reference not set to an instance of an object" error and I am left with localfile being 0kb.

    I am already checking if xmlData is not null - so I can't figure out how this is happening. But it's as if xmlData has suddenly become null right before FileIO.WriteTextAsync.

    Any help would be greatly appreciated!

    Tuesday, February 24, 2015 1:47 PM

Answers

  • Hi Ryan,

    The error message sounds like something is still null while trying to assign value to it.

    Is it possible that the file or the xamlData is not ready, try to test them before writing things to file. Besides since the issue is very very occasionally, looks like an environment issue, I would suggest you to work with try-catch and if the error occurs re-write again should be ok.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ryan Cairns Wednesday, February 25, 2015 12:36 PM
    Wednesday, February 25, 2015 7:00 AM
    Moderator

All replies

  • Hi Ryan,

    The error message sounds like something is still null while trying to assign value to it.

    Is it possible that the file or the xamlData is not ready, try to test them before writing things to file. Besides since the issue is very very occasionally, looks like an environment issue, I would suggest you to work with try-catch and if the error occurs re-write again should be ok.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ryan Cairns Wednesday, February 25, 2015 12:36 PM
    Wednesday, February 25, 2015 7:00 AM
    Moderator
  • Hi James - yes I would say you are right. I will write the file again in my catch block.

    Thanks
    Wednesday, February 25, 2015 12:35 PM