Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error - Object reference not set to an instance of an object

Status
Not open for further replies.

sunil128

Programmer
Mar 20, 2008
45
GB
Hi all I have another newbie question! Apologies in advacne for my poor explaination of the problem but please bear with me.

Anyway I am getting the following error with my application - a VS2005 app written in C# with code behind:

“NullReferenceException was unhandled by user code
Object reference not set to an instance of an object.”

Before I go into great detail about the problem here is the code, firstly the code behind for the aspx – CallLogDetail.aspx.cs. For info the error is occurring with m_callog.

Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.ComponentModel;
//using System.Web.SessionState;

namespace Property
{        
    
    public partial class CallLogDetail : System.Web.UI.Page
    //public partial class CallLogDetail : CallLogDetail
    {
        protected CallLog m_CallLog;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //populate the branch drop down list(s)
                DataSet ds = Branch.GetDDLData();
                DDLBranch.Items.Add(new ListItem("Not specified", "0"));
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        DDLBranch.Items.Add(new ListItem(dr["Description"].ToString(), dr["ID"].ToString()));
                    }
                }



        private void Populate()
        {
            //int i;
            //string s;
            DDLBranch.SelectedValue = m_CallLog.LocationID.ToString();
            DDLBranchNo.SelectedValue = DDLBranch.SelectedValue;
            //s = DDLBranch.Items.
            txtBranch.Text = m_CallLog.LogID.ToString();
            txtArea.Text = m_CallLog.Area.ToString();
            txtEnteredBy.Text = m_CallLog.EnteredBy;
            txtEnteredDate.Text = m_CallLog.EnteredDate.ToString();
            txtReportedBy.Text = m_CallLog.ReportedBy;
            txtProblemDetails.Text = m_CallLog.ProblemText;
            txtInternalComments.Text = m_CallLog.Comments;
            if (m_CallLog.OrderID != "0") txtOrderID.Text = m_CallLog.OrderID;
            txtContractor.Text = m_CallLog.Contractor;
            if (m_CallLog.DealtWith.ToShortDateString() != "01/01/0001") txtDealtWith.Text = m_CallLog.DealtWith.ToShortDateString();
            if (m_CallLog.FixBy.ToShortDateString() != "01/01/0001") txtFixBy.Text = m_CallLog.FixBy.ToShortDateString();
            if (m_CallLog.DateCompleted.ToShortDateString() != "01/01/0001") txtDateCompleted.Text = m_CallLog.DateCompleted.ToShortDateString();
            txtConfirmedBy.Text = m_CallLog.ConfirmedBy;
            

        }



        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Update();
        }

        private void Update()
        {
            bool blnOK = false;

            m_CallLog.EnteredBy = txtEnteredBy.Text;
            m_CallLog.EnteredDate = Convert.ToDateTime(txtEnteredDate.Text);
            m_CallLog.LocationID = 1;
            m_CallLog.ProblemText = txtProblemDetails.Text;
            m_CallLog.OrderID = txtOrderID.Text;
            m_CallLog.DealtWith = Convert.ToDateTime(txtDealtWith.Text);
            m_CallLog.FixBy = Convert.ToDateTime(txtFixBy.Text);
            m_CallLog.DateCompleted = Convert.ToDateTime(txtDateCompleted.Text);
            m_CallLog.ConfirmedBy = txtConfirmedBy.Text;
            //m_CallLog.StatusID;
            m_CallLog.Comments = txtInternalComments.Text;
            m_CallLog.ReportedBy = txtReportedBy.Text;
            m_CallLog.Update();

            blnOK = true;

            if (blnOK == true)
            {
                //if (sender.GetType() == ucEdit.GetType()) ReturnToEnquiry();
            }
        }



Now here is the call log class Calllog.cs

Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

/// <summary>
/// Summary description for CallLogDetail
/// </summary>
/// 

namespace Property
{ 
    public class CallLog
    {
            //
            // TODO: Add constructor logic here
            //
        private int m_LogID = 0;
		private String m_EnteredBy;
        private DateTime m_EnteredDate;
		private int m_LocationID;
        private String m_BranchName;
        private int m_Area;
		private string m_ProblemText;
        private string m_OrderID;
        private string m_Contractor;
        private DateTime m_DealtWith;
        private DateTime m_FixBy; 
        private DateTime m_DateCompleted;
        private string m_ConfirmedBy;
        private int m_StatusID;
        private string m_Comments;
        private DateTime m_ProjectDate;
        private string m_ReportedBy;
        private bool m_DirtyFlag = false;

        public CallLog()
		{
		}

        //retrives all the info for a single call log
        public CallLog(int intID)
		{
			try
			{
                using (SqlCommand sqlCmd = new SqlCommand("CallsDetail", DB.OpenConnection())) 
				{
					sqlCmd.CommandType = CommandType.StoredProcedure;

					sqlCmd.Parameters.Add("@LogID", SqlDbType.Int).Value = intID;

					using (SqlDataReader rdrSQL = sqlCmd.ExecuteReader()) 
					{
						while (!rdrSQL.IsClosed && rdrSQL.Read())
						{
                            m_LogID = rdrSQL.GetInt32(0);
                            m_EnteredBy = rdrSQL.GetString(1);
                            m_EnteredDate = rdrSQL.GetDateTime(2);
                            m_LocationID = rdrSQL.GetInt32(3);
                            m_BranchName = rdrSQL.GetString(4);
                            if (!rdrSQL.IsDBNull(5)) m_Area = rdrSQL.GetInt32(5);
                            m_ProblemText = rdrSQL.GetString(6);
                            if (!rdrSQL.IsDBNull(7)) m_OrderID = rdrSQL.GetString(7);
                            if (!rdrSQL.IsDBNull(8)) m_Contractor = rdrSQL.GetString(8);
                            if (!rdrSQL.IsDBNull(9)) m_DealtWith = rdrSQL.GetDateTime(9);
                            if (!rdrSQL.IsDBNull(10)) m_FixBy = rdrSQL.GetDateTime(10);
                            if (!rdrSQL.IsDBNull(11)) m_DateCompleted = rdrSQL.GetDateTime(11);
                            if (!rdrSQL.IsDBNull(12)) m_ConfirmedBy = rdrSQL.GetString(12);
                            m_StatusID = rdrSQL.GetInt32(13);
                            if (!rdrSQL.IsDBNull(14)) m_Comments = rdrSQL.GetString(14);
                            if (!rdrSQL.IsDBNull(15)) m_ProjectDate = rdrSQL.GetDateTime(15);
                            m_ReportedBy = rdrSQL.GetString(16);
						}
					}

					DB.NonQuery(sqlCmd);

					sqlCmd.Parameters.Clear();
				}


			}
			catch
			{
				throw;
			}
		}



        public void Dirty()
        {
            m_DirtyFlag = true;
        }

        public void UnDirty()
        {
            m_DirtyFlag = false;
        }


        public void Update()
        {
            try
            {
                if (m_DirtyFlag == true)
                {
                    using (SqlCommand sqlCmd = new SqlCommand())
                    {
                        sqlCmd.CommandType = CommandType.StoredProcedure;

                        sqlCmd.CommandText = "CallUpdate";

                        sqlCmd.Connection = DB.OpenConnection();

                        sqlCmd.Parameters.Add("@LogID", SqlDbType.Int).Value = m_LogID;
                        //sqlCmd.Parameters["@ID"].Direction = ParameterDirection.InputOutput;
                        sqlCmd.Parameters.Add("@Enteredby", SqlDbType.VarChar).Value = m_EnteredBy;
                        sqlCmd.Parameters.Add("@Entered", SqlDbType.DateTime).Value = m_EnteredDate;
                        sqlCmd.Parameters.Add("@LocationID", SqlDbType.Int).Value = m_LocationID;
                        sqlCmd.Parameters.Add("@ProblemText", SqlDbType.VarChar).Value = m_ProblemText;
                        sqlCmd.Parameters.Add("@OrderID", SqlDbType.VarChar).Value = m_OrderID;
                        sqlCmd.Parameters.Add("@DealtWith", SqlDbType.DateTime).Value = m_DealtWith;
                        sqlCmd.Parameters.Add("@FixBy", SqlDbType.DateTime).Value = m_FixBy;
                        sqlCmd.Parameters.Add("@DateCompleted", SqlDbType.DateTime).Value = m_DateCompleted;
                        sqlCmd.Parameters.Add("@ConfirmedBy", SqlDbType.VarChar).Value = m_ConfirmedBy;
                        sqlCmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = m_StatusID;
                        sqlCmd.Parameters.Add("@Comments", SqlDbType.VarChar).Value = m_Comments;
                        sqlCmd.Parameters.Add("@ReportedBy", SqlDbType.VarChar).Value = m_ReportedBy;

                        DB.NonQuery(sqlCmd);

                        //m_ID = (int)sqlCmd.Parameters["@ID"].Value;
                        //m_TStamp = (DateTime)sqlCmd.Parameters["@TStamp"].Value;

                        m_DirtyFlag = false;

                        sqlCmd.Parameters.Clear();
                        sqlCmd.Connection.Dispose();
                    }
                }
            }
            catch
            {
                throw;
            }
        }



        //Retrieves summary of all calls for CallLogEnquiry Screen
        public static DataSet GetEnquiryData(int intLocationID, int intStatusID,
                    int intArea, int intContractor, string strUser,int intPage, int intNumPerPage)
        {
            try
            {
                DataSet dsOrder = new DataSet();
                using (SqlCommand sqlCom = new SqlCommand("CallsEnquiry", DB.OpenConnection()))
                {
                    sqlCom.CommandType = CommandType.StoredProcedure;

                    sqlCom.Parameters.Add("@LocationID", SqlDbType.Int).Value = intLocationID;
                    sqlCom.Parameters.Add("@StatusID", SqlDbType.Int).Value = intStatusID;
                    sqlCom.Parameters.Add("@Area", SqlDbType.Int).Value = intArea;
                    sqlCom.Parameters.Add("@Contractor", SqlDbType.Int).Value = intContractor;
                    sqlCom.Parameters.Add("@EnteredBy", SqlDbType.VarChar).Value = strUser;
                    sqlCom.Parameters.Add("@Page", SqlDbType.Int).Value = intPage;
                    sqlCom.Parameters.Add("@NumPerPage", SqlDbType.Int).Value = intNumPerPage;

                    //dsOrder = DB.DataSetQuery(sqlCom);
                    
                    return DB.DataSetQuery(sqlCom);
                    
                    sqlCom.Parameters.Clear();
                }

                
            }
            catch
            {
                throw;
            }
        }





        public int LogID
        {
            get { return m_LogID; }
            set { m_LogID = value; m_DirtyFlag = true; }
        }

        public string EnteredBy
        {
            get { return m_EnteredBy; }
            set { m_EnteredBy = value; m_DirtyFlag = true; }
        }

        public DateTime EnteredDate
        {
            get { return m_EnteredDate; }
            set { m_EnteredDate = value; m_DirtyFlag = true; }
        }

        public int LocationID
        {
            get { return m_LocationID; }
            set { m_LocationID = value; m_DirtyFlag = true; }
        }

        public int Area
        {
            get { return m_Area; }
            set { m_Area = value; m_DirtyFlag = true; }
        }

        public string ProblemText
        {
            get { return m_ProblemText; }
            set { m_ProblemText = value; m_DirtyFlag = true; }
        }

        public string OrderID
        {
            get { return m_OrderID; }
            set { m_OrderID = value; m_DirtyFlag = true; }
        }

        public string Contractor
        {
            get { return m_Contractor; }
            set { m_Contractor = value; m_DirtyFlag = true; }
        }

        public DateTime DealtWith
        {
            get { return m_DealtWith; }
            set { m_DealtWith = value; m_DirtyFlag = true; }
        }

        public DateTime FixBy
        {
            get { return m_FixBy; }
            set { m_FixBy = value; m_DirtyFlag = true; }
        }

        public DateTime DateCompleted
        {
            get { return m_DateCompleted; }
            set { m_DateCompleted = value; m_DirtyFlag = true; }
        }

        public string ConfirmedBy
        {
            get { return m_ConfirmedBy; }
            set { m_ConfirmedBy = value; m_DirtyFlag = true; }
        }

        public int StatusID
        {
            get { return m_StatusID; }
            set { m_StatusID = value; m_DirtyFlag = true; } 
        }

        public string Comments
        {
            get { return m_Comments; }
            set { m_Comments = value; m_DirtyFlag = true; }
        }

        public DateTime ProjectDate
        {
            get { return m_ProjectDate; }
            set { m_ProjectDate = value; m_DirtyFlag = true; }
        }

        public string ReportedBy
        {
            get { return m_ReportedBy; }
            set { m_ReportedBy = value; m_DirtyFlag = true; }
        } 

    }
}



The error occurs in the code behind when it first tries to set a property of the instantiated calllog class i.e.

m_CallLog.EnteredBy = txtEnteredBy.Text;


If I hover over m_calllog on that line whilst stepping thru it says m_calllog is null, it also says it is null at the actual instatiation line i.e. protected CallLog m_CallLog;

However I have basically replicated this code from existing code which works fine so I am very puzzled. can someone tell me what schoolboy error ive made here?

Thanks





 
You probably want:
Code:
protected CallLog m_CallLog = new CallLog();
Regards

Nelviticus
 
Many thanks Nelviticus, that worked! I should have actually attempted that myself before as it was one of the suggestions!

But im still confused as essentially the same code works elsewhere in the application without declaring it as new. Can you explain this?
 
CallLog is an object.
m_CallLog is an instance of that object.
none of the CallLog members are static. so you need an instance of the object to reference it's members.

If a member of CallLog, or the entire call log object been static. then you could do
CallLog.DoSomething(); without instantiating the object itself.
most of the time you do not need a static member/class.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top