C#WinForms应用程序中的字典导致空引用(再次)

C#WinForms应用程序中的字典导致空引用(再次),c#,winforms,visual-studio,dictionary,C#,Winforms,Visual Studio,Dictionary,不同的问题 我正在使用Visual Studio 2013创建Visual C#Windows窗体应用程序,而不是使用设计器来设置窗体 我正在尝试使用字典来存储位图,以便以后可以直呼其名。但是,当我调试脚本时,会出现以下错误: An unhandled exception of type 'System.NullReferenceException' occurred in SimpleForm.exe Additional information: Object reference not s

不同的问题

我正在使用Visual Studio 2013创建Visual C#Windows窗体应用程序,而不是使用设计器来设置窗体

我正在尝试使用字典来存储位图,以便以后可以直呼其名。但是,当我调试脚本时,会出现以下错误:

An unhandled exception of type 'System.NullReferenceException' occurred in SimpleForm.exe
Additional information: Object reference not set to an instance of an object.
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 bmpTemp;
        private Dictionary<string, Bitmap> dirStrBmp;

        public Test()
        {
            bmpTemp = new Bitmap("graphic.png");
            dirStrBmp.Add("TempImageName", bmpTemp);
        }

    }

}
从这一行:

dirStrBmp.Add("TempImageName", bmpTemp);
任何帮助都将不胜感激

减少仍会产生错误的代码版本:

An unhandled exception of type 'System.NullReferenceException' occurred in SimpleForm.exe
Additional information: Object reference not set to an instance of an object.
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 bmpTemp;
        private Dictionary<string, Bitmap> dirStrBmp;

        public Test()
        {
            bmpTemp = new Bitmap("graphic.png");
            dirStrBmp.Add("TempImageName", bmpTemp);
        }

    }

}
使用系统;
使用系统图;
使用系统集合;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Windows.Forms;
使用系统数据;
名称空间SimpleForm
{
公开课考试:表格
{
静态位图bmpTemp;
私人词典dirStrBmp;
公开考试()
{
bmpTemp=新位图(“graphic.png”);
dirStrBmp.Add(“tempmagename”,bmpTemp);
}
}
}

修改字典的声明如下:

 private Dictionary<string, Bitmap> dirStrBmp = new Dictionary<string,Bitmap>();
private Dictionary dirStrBmp=new Dictionary();

修改字典的声明如下:

 private Dictionary<string, Bitmap> dirStrBmp = new Dictionary<string,Bitmap>();
private Dictionary dirStrBmp=new Dictionary();

您的词典未实例化…您的词典未实例化。。。