9

Visual Studio gives NullReferenceException by trying to add my control!

 3 years ago
source link: https://www.codesd.com/item/visual-studio-gives-nullreferenceexception-by-trying-to-add-my-control.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Visual Studio gives NullReferenceException by trying to add my control!

advertisements

Here's the error:

gZbqb.jpg

It says the source of the exception is on Line20 of my class. Here is my class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace WebServiceScanner
{
    public partial class imageList : UserControl
    {
        private int XPosition = 0;

        public imageList()
        {
            InitializeComponent();
            Images.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Images_CollectionChanged);
        }       

        public ObservableCollection<selectablePicture> Images { get; set; }

        public void AddImage(selectablePicture image)
        {
            Images.Add(image);
        }

        public void RemoveImage(selectablePicture image)
        {
            Images.Remove(image);
        }

        public void MoveImageLeft(int index)
        {
            selectablePicture tmpImage = Images[index];
            Images[index] = Images[index - 1];
            Images[index - 1] = tmpImage;
        }

        public void MoveImageRight(int index)
        {
            selectablePicture tmpImage = Images[index];
            Images[index] = Images[index + 1];
            Images[index + 1] = tmpImage;
        }

        void Images_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RedrawImages();
        }

        private void RedrawImages()
        {
            foreach (var picture in Images)
            {
                picture.Location = new Point(XPosition + panel1.AutoScrollPosition.X, 0);
                XPosition += 130;
                panel1.Controls.Add(picture);
            }
        }
    }
}

Maybe I'm doing something dumb like setting an event handler at the constructor. Any ideas? This user control isn't really that complex, so there are few places where something could be wrong.

If you guys need more information please let me know.


Add a statement to instantiate Images.

public imageList()
{
    InitializeComponent();

    Images = new ObservableCollection<selectablePicture>();
    Images.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Images_CollectionChanged);
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK