Controls-ReaderPanel icon indicating copy to clipboard operation
Controls-ReaderPanel copied to clipboard

System.NullReferenceException:“Object reference not set to an instance of an object.”

Open ghost opened this issue 5 years ago • 0 comments

大佬好,最近发现了一个比较奇怪的issue (也可能是我使用的问题)

如果使用FilePicker然后再Reader.OpenAsync(file, new ReaderStyle());不会报错,但是如果打开LocalState下的Books文件夹里的书就会报错。

报错内容

System.NullReferenceException:“Object reference not set to an instance of an object.”

这样就没问题

StorageFile file = await App._instance.IO.OpenLocalFileAsync(".txt", ".epub");
await Reader.OpenAsync(file, new ReaderStyle());

这样就会报错

StorageFile file = await IOHelper.GetStorageFile($"{_item.Name}.{BookHelper.ConvertToExtension(_item.Type)}", "Books");
await Reader.OpenAsync(file, new ReaderStyle());

使用的IOHelper代码:

public class IOHelper
    {
        public static async Task<StorageFolder> GetStorageFolder(string folderName = "")
        {
            StorageFolder folder;
            if (string.IsNullOrEmpty(folderName))
                folder = ApplicationData.Current.LocalFolder;
            else
                folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);
            return folder;
        }

        public static async Task<StorageFile> GetStorageFile(string fileName, string folderName = "")
        {
            StorageFolder folder = await GetStorageFolder(folderName);
            StorageFile file = await folder.GetFileAsync(fileName);
            return file;
        }
}

ghost avatar Dec 22 '20 04:12 ghost