Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

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

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 ?

User generated image
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

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

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