C#: WinForms User Control Object reference not set to an instance of an object

trevor1940
trevor1940 used Ask the Experts™
on
Hi

Using the TMDB API
I have a User Control on a winform with a TvTitleLbl Label & TvPosterPictureBox

I've been strangling to extract the year from KnownForTV.FirstAirDate having finally got it working, is this the best way?
I'm now getting Error, "Object reference not set to an instance of an object" on both the Label & PictureBox when trying to set it's properties unsure why ?

TvTit.jpg
the proper lable.text will be "TvTitleLbl.Text = "TV: " + pKFTV.Name + " ( " + year.ToString() + " )";"  shortened it to debug

KnownForTV.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MoviesDataModel;
using TMDbLib.Client;
using TMDbLib.Objects.General;
using TMDbLib.Objects.Movies;
using TMDbLib.Objects.TvShows;
using TMDbLib.Objects.Search;
using TMDbLib.Objects.People;

namespace FilmsDB
{
    public partial class KnownForTV : UserControl
    {
        TMDbClient client = new TMDbClient("API KEY");
        const string ImgPath = @"E:\Media\thumbs\Movies\";
        const string ImgURL = "https://image.tmdb.org/t/p/";
        const string OriginalImgPath = "http://image.tmdb.org/t/p/original";
        public string ThumbPhotoPath { get; set; }
        public string FullPhotoPath { get; set; }
        private KnownForTv pKFTV;



        public KnownForTV(KnownForTv pKFTV)
        {
            this.pKFTV = pKFTV;
            if (pKFTV.PosterPath != null)
            {
                ThumbPhotoPath = ImgPath + "JohnWayneSml.png";
            }
            else
            {
                ThumbPhotoPath = ImgURL + "w92" + pKFTV.PosterPath;
                FullPhotoPath = ImgURL + "original" + pKFTV.PosterPath;
            }
             // Casting didn't work nor did FirstAirDate.year
            //var FAD = (FirstAirDate)pKFTV;
            // FirstAirDate is a System.DateTime object so why dose it need converting to a string first?
            var dateTime = DateTime.Parse(pKFTV.FirstAirDate.ToString());
            int year = dateTime.Year;
            TvTitleLbl.Text = "TV: " + pKFTV.Name + " ( " + year.ToString() + " )";
            TvPosterPictureBox.LoadAsync(ThumbPhotoPath);
        }

        private void TvPosterPictureBox_MouseHover(object sender, EventArgs e)
        {
            if (FullPhotoPath != null)
            {
                string Orientation = "P";
                FilmsDB.PictureForm frm = new PictureForm(Orientation);
                frm.SetValues(FullPhotoPath);
                frm.ShowDialog();
            }
        }
    }
}

Open in new window


KnownForTV.Designer.cs

namespace FilmsDB
{
    partial class KnownForTV
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.TvPosterPictureBox = new System.Windows.Forms.PictureBox();
            this.TvTitleLbl = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.TvPosterPictureBox)).BeginInit();
            this.SuspendLayout();
            // 
            // TvPosterPictureBox
            // 
            this.TvPosterPictureBox.Location = new System.Drawing.Point(21, 32);
            this.TvPosterPictureBox.Name = "TvPosterPictureBox";
            this.TvPosterPictureBox.Size = new System.Drawing.Size(92, 138);
            this.TvPosterPictureBox.TabIndex = 5;
            this.TvPosterPictureBox.TabStop = false;
            this.TvPosterPictureBox.MouseHover += new System.EventHandler(this.TvPosterPictureBox_MouseHover);
            // 
            // TvTitleLbl
            // 
            this.TvTitleLbl.AutoSize = true;
            this.TvTitleLbl.Location = new System.Drawing.Point(16, 13);
            this.TvTitleLbl.Name = "TvTitleLbl";
            this.TvTitleLbl.Size = new System.Drawing.Size(35, 13);
            this.TvTitleLbl.TabIndex = 6;
            this.TvTitleLbl.Text = "label1";
            // 
            // KnownForTV
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.TvTitleLbl);
            this.Controls.Add(this.TvPosterPictureBox);
            this.Name = "KnownForTV";
            this.Size = new System.Drawing.Size(128, 186);
            ((System.ComponentModel.ISupportInitialize)(this.TvPosterPictureBox)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.PictureBox TvPosterPictureBox;
        private System.Windows.Forms.Label TvTitleLbl;
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Consultant
Top Expert 2016
Commented:
This is a known behavior when you are trying to access controls that have not been created yet. Instead of having code in the form's constructor accessing controls, you better use an form's event like Shown to access the controls.

Author

Commented:
Feel stupid now I had forgotten to add this line
"  InitializeComponent();"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial