我正在使用Visual Studio 2013来创建Visual C#Windows窗体应用程序,而我没有使用Designer来设置窗体.
我正在尝试使用Dictionary来存储位图,以便我可以稍后通过名称调用它们.但是当我调试脚本时,我收到错误:
An unhandled exception of type 'System.NullReferenceException' occurred in SimpleForm.exe
Additional information: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
从行:
width = imgLetters["a"].Width;
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
减少仍会产生错误的代码版本:
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> …Run Code Online (Sandbox Code Playgroud)