简体   繁体   English

具有非空值的空异常

[英]null exception with not null value

So I have this method here: 所以我在这里有这种方法:

    public async Task<PixelData> GrabPixelData(string imageFileName)
    {
        if (!ImageDictionary.ContainsKey(imageFileName))
        {
            // doesn't exist yet, so load it
            PixelData pd = await LoadPic(imageFileName);
            ImageDictionary.Add(imageFileName, pd);
        }

        var test = ImageDictionary[imageFileName];

        return ImageDictionary[imageFileName];
    }

The debugger says "test" contains an object of type PixelData (with real, non-static values). 调试器说“测试”包含类型为PixelData的对象(具有真实的非静态值)。

When it returns to the calling method, however, it says there is a null reference exception on that line: 但是,当返回调用方法时,它说该行上存在一个空引用异常:

    private async void LoadPic()
    {
        myObject.pixelData = await rootPage.GrabPixelData("obj1.png");
    }

MyObject is not null either (according to the debugger) ... MyObject也不为空(根据调试器)...

Is it that a Task gets returned? 是返回任务吗?

EDIT: 编辑:

ImageDictionary is Dictionary. ImageDictionary是字典。

Change your LoadPic function to return Task : 更改您的LoadPic函数以返回Task

private async Task LoadPic()
{
    myObject.pixelData = await rootPage.GrabPixelData("obj1.png");
}

Your method GrabPixelData clearly returns a Task<PixelData > instance and not PixelData . 您的方法GrabPixelData显然返回Task<PixelData >实例,而不PixelData So yes, your guess is correct, it is returning a Task instance. 是的,您的猜测是正确的,它正在返回Task实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM