0

So I have had an error "Object reference not set to an instance of an object".

Here is the area where the error is occurring from:

private async void Savebutton_click(object sender, RoutedEventArgs e)
{
    string recipename = recipenametextbox.Text.ToString();
    string recipemethod = methodtextbox.Text.ToString();
    string recipeingredients = ingredientstextbox.Text.ToString();

    //File names.
    string recipenamefilename = recipename + "title.txt";
    string recipemethodfilename = recipename + "method.txt";
    string recipeingredientsfilename = recipename + "ingredients.txt";

    await currentrecipe.folder.CreateFolderAsync(recipename);

    await currentrecipe.folder.CreateFileAsync(recipenamefilename);
    File.WriteAllText(recipenamefilename, recipename);

    await currentrecipe.folder.CreateFileAsync(recipemethodfilename);
    File.WriteAllText(recipemethodfilename, recipemethod);

    await currentrecipe.folder.CreateFileAsync(recipeingredientsfilename);
    File.WriteAllText(recipeingredientsfilename, recipeingredients);

    //Clear currentrecipe ready for next recipe.
    currentrecipe.folder = null;
    currentrecipe.Picturefile = null;
    currentrecipe.recipebackgroundcolourenabled = false;
}

I am presuming that it is something to do with initialising the currentrecipe variable's StorageFolder member (currentrecipe is a recipie varible). Also I don't think it is a duplicate of the NullReferanceException question, simply due to the fact that I know what the NullReferanceException is, and I know how to fix it in a way, but I do not know how to fix it for this particular NullReferanceException - for Storage Folders. Also the error happens at await currentrecipe.folder.CreateFolderAsync(recipename).

Here is my recipie class:

public sealed class recipie
{
    public StorageFile Picturefile;
    public StorageFolder folder;
    public bool recipebackgroundcolourenabled;
}
rj60001
  • 13
  • 3
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Herdo Sep 10 '15 at 07:14
  • You should add a breakpoint at the top of your function and step through it one row at a time, that way you can see exactly at what line your null reference happens. – Tobbe Sep 10 '15 at 07:53
  • I don't think it is a duplicate, simply due to the fact that I know what the NullReferanceException is, and I know how to fix it in a way, but I do not know how to fix it for this particular NullReferanceException - for Storage Folders. Also the error happens at await currentrecipe.folder.CreateFolderAsync(recipename). @Tobbe – rj60001 Sep 10 '15 at 16:11

1 Answers1

0

As you say the error happens at

await currentrecipe.folder.CreateFolderAsync(recipename)

I'd first check that both currentrecipe and folder are initialized:

if (currentrecipe != null)
{
    if (currentrecipe.folder != null)
    {
        await currentrecipe.folder.CreateFolderAsync(recipename)
    }
    else
    {
        Console.Write("currentrecipe.folder is null")
    }
}   
else
{
    Console.Write("currentrecipe is null")
}

At least start with that, otherwise it could be that CreateFolderAsync returns null.

Tobbe
  • 1,757
  • 3
  • 18
  • 36