简体   繁体   中英

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).

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) ...

Is it that a Task gets returned?

EDIT:

ImageDictionary is Dictionary.

Change your LoadPic function to return Task :

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

Your method GrabPixelData clearly returns a Task<PixelData > instance and not PixelData . So yes, your guess is correct, it is returning a Task instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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