micahhoover
micahhoover

Reputation: 2160

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.

Upvotes: 2

Views: 109

Answers (2)

James Poulose
James Poulose

Reputation: 3833

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

Upvotes: 0

chue x
chue x

Reputation: 18823

Change your LoadPic function to return Task:

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

Upvotes: 1

Related Questions