Windows 如何使用StorageFolder变量类型创建新对象?
因此,我有一个错误对象引用没有设置为对象的实例 以下是发生错误的区域:Windows 如何使用StorageFolder变量类型创建新对象?,windows,uwp,Windows,Uwp,因此,我有一个错误对象引用没有设置为对象的实例 以下是发生错误的区域: private async void Savebutton_click(object sender, RoutedEventArgs e) { string recipename = recipenametextbox.Text.ToString(); string recipemethod = methodtextbox.Text.ToString(); string recipeingredien
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;
}
我认为这与初始化currentrecipe变量的StorageFolder成员有关currentrecipe是一个recipie变量。此外,我不认为这是与NullReferenceException问题的重复,因为我知道NullReferenceException是什么,我知道如何以某种方式修复它,但我不知道如何修复这个特定的NullReferenceException-存储文件夹。该错误也发生在Wait currentrecipe.folder.CreateFolderAsyncrecipename上 这是我的recipie课程:
正如你所说,错误发生在
await currentrecipe.folder.CreateFolderAsync(recipename)
我首先检查currentrecipe和folder是否都已初始化:
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")
}
至少从这一点开始,否则可能是CreateFolderAsync返回null。可能的重复应该在函数顶部添加一个断点,一次一行地遍历它,这样你就可以准确地看到null引用发生在哪一行。我不认为它是重复的,这仅仅是因为我知道NullReferenceException是什么,并且我知道如何以某种方式修复它,但我不知道如何修复存储文件夹的这个特定NullReferenceException。该错误也发生在Wait currentrecipe.folder.CreateFolderAsyncrecipename上@托比
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")
}