将InMemoryRandomAccessStream添加到列表时出现NullReferenceException

时间:2017-11-15 21:44:53

标签: c# windows list uwp stream

        private List<InMemoryRandomAccessStream> undoStreamList;            
        InMemoryRandomAccessStream tempStream = new InMemoryRandomAccessStream();
        await inkCanvas.InkPresenter.StrokeContainer.SaveAsync(tempStream);
        undoStreamList.Add(tempStream);
        state++;

此代码在倒数第二行返回NullReferenceException,我该如何解决?

我也尝试将倒数第二行更改为undoStreamList.Add(new InMemoryRandomAccessStream);,但它不起作用。

1 个答案:

答案 0 :(得分:1)

看起来您的列表尚未初始化:

// Initialize this either inline or inside the constructor...
private List<InMemoryRandomAccessStream> undoStreamList = new List<InMemoryRandomAccessStream>();

InMemoryRandomAccessStream tempStream = new InMemoryRandomAccessStream();
await inkCanvas.InkPresenter.StrokeContainer.SaveAsync(tempStream);
undoStreamList.Add(tempStream);
state++;

这是该行抛出异常的唯一可能原因!