1

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;

        }


    }

}
RebDev
  • 335
  • 1
  • 5
  • 13
  • You have two variables called `imgLetters`, one is visible in the whole class, the second one is created in `ImgInitialize`. What's happening is: When you assign a value to `ImgLetters`, you assign to the variable/object specified in the `ImgInitialize`, not the private one in class. This means that the imgLetters in class have the default value, which is `NULL`, remove the declaration of imgLetters from `ImgInitialize` or use the `this` keyword as suggested in one of the answers. – mishan Nov 14 '13 at 13:09

4 Answers4

6

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.
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
3

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.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
3

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.

Community
  • 1
  • 1
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
3

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208