Dictionary in C# WinForms App causing null referen

2019-08-08 20:55发布

I'm using Visual Studio 2013 to create a Visual C# Windows Forms Application and I'm not using the Designer to setup the form.

I'm trying to use a Dictionary to store Bitmaps so that I can call them later by name. But when I debug the script I get the error:

An unhandled exception of type 'System.NullReferenceException' occurred in SimpleForm.exe
Additional information: Object reference not set to an instance of an object.

From the line:

width = imgLetters["a"].Width;

Any help would be greatly appreciated.

Cut down version of code which still produces the error:

using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SimpleForm
{

    public class Test : Form
    {

        static Bitmap bmpLetterA;
        static Bitmap bmpLetterB;
        static Bitmap bmpLetterC;
        private Dictionary<string, Bitmap> imgLetters;

        public Test()
        {

            ImgInitialize();
            ImgWidth();

        }

        private void ImgInitialize()
        {

            Dictionary<string, Bitmap> imgLetters;

            bmpLetterA = new Bitmap("a.png");
            bmpLetterB = new Bitmap("b.png");
            bmpLetterC = new Bitmap("c.png");

            imgLetters = new Dictionary<string, Bitmap>();

            imgLetters.Add("a", bmpLetterA);
            imgLetters.Add("b", bmpLetterB);
            imgLetters.Add("c", bmpLetterC);

        }

        private void ImgWidth()
        {

            int width = 0;
            width = imgLetters["a"].Width;

        }


    }

}

4条回答
▲ chillily
2楼-- · 2019-08-08 21:07

The problem is in your ImgInitialize() method.

remove this Line

Dictionary<string, Bitmap> imgLetters;

What you are doing is creating a local variable with the same name as your global variable. So it is this one that you are adding values to. This then lost when the method has completed due to its scope.

Your global variable is still null at this point and this is why the error occurs.

查看更多
何必那么认真
3楼-- · 2019-08-08 21:13

Friendly tip to prevent such mistakes in the future: when using a class member, append this:

this.imgLetters = new Dictionary<string, Bitmap>();
this.imgLetters.Add("a", bmpLetterA);
this.imgLetters.Add("b", bmpLetterB);
this.imgLetters.Add("c", bmpLetterC);

This way, even if you have local variable with the same name it won't interfere.

查看更多
Lonely孤独者°
4楼-- · 2019-08-08 21:16

Remove the line Dictionary<string, Bitmap> imgLetters; from ImgInitialize. This creates a local variable with the same name as the member variable. It is then filled, but never used, while the member variable remains uninitialized.

Tipps for avoiding problems like this:

  1. You could name instance members in a special way to make clear the variable is an instance member (for example m_member instead of member).
  2. You could prefix access to an instance member with this. to make clear which variable you want to access.
  3. You could try to avoid naming local variables the same as instance members.
查看更多
叼着烟拽天下
5楼-- · 2019-08-08 21:24

The problems lies here:

private void ImgInitialize()
{
    Dictionary<string, Bitmap> imgLetters;

This variable shadows the class's field private Dictionary<string, Bitmap> imgLetters;. Remove this declaration from the method ImgInitialize and it'll work fine.

查看更多
登录 后发表回答