1

I am currently working on an system using ASP.Net Webforms and i have this code on one of my page:

namespace mypms.View
{
    public partial class Reports : System.Web.UI.Page
    {
        private string truck_id = null;

        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

            truck_id = this.Request.QueryString["u"] != null ? this.Request.QueryString["u"] : Session["truck_id"] != null ? Session["truck_id"].ToString() : "";
            connectDB();
        }

        private void connectDB()
        {
            try
            {
                conn.Open();

                string selectUserQuery = @"SELECT * FROM thesis.joborder as jo inner join thesis.jobordermaterials as jom ON jom.jobID = jo.jobId Where jom.truck_id = '" + truck_id + "'";
                MySqlCommand cmd = new MySqlCommand(selectUserQuery, conn);

                foreach (DbDataRecord rowData in cmd.ExecuteReader())
                {
                    populateNewTableRow(rowData);
                }
            }
            catch (MySqlException ex)
            {
                System.Diagnostics.Debug.WriteLine("ERROR: " + ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }

        private void populateNewTableRow(DbDataRecord rowData)
        {
            TableRow tr = new TableRow();

            TableCell cell2 = new TableCell();
            cell2.Text = rowData.GetInt32(rowData.GetOrdinal("jobId")).ToString();
            tr.Cells.Add(cell2);

            TableCell cell3 = new TableCell();
            cell3.Text = rowData.GetString(rowData.GetOrdinal("odometerReading")).ToString();
            tr.Cells.Add(cell3);

            TableCell cell4 = new TableCell();
            cell4.Text = rowData.GetString(rowData.GetOrdinal("description")).ToString();
            tr.Cells.Add(cell4);

            TableCell cell5 = new TableCell();
            cell5.Text = rowData.GetInt32(rowData.GetOrdinal("quantity")).ToString();
            tr.Cells.Add(cell5);

            TableCell cell6 = new TableCell();
            cell6.Text = rowData.GetString(rowData.GetOrdinal("remarks")).ToString();
            tr.Cells.Add(cell6);

            TableCell cell7 = new TableCell();
            cell7.Text = rowData.GetString(rowData.GetOrdinal("mechanic")).ToString();
            tr.Cells.Add(cell7);

            tbl_MaitenanceRecord.Rows.Add(tr);// THIS (Null Reference Exception)
        }
}

I am receiving an error System.NullReferenceException: Object reference not set to an instance of an object. on this line

tbl_MaitenanceRecord.Rows.Add(tr);

I am quite sure that my codes are correct. What could be the cause of this Null Reference Exception? Thanks in advance

1 Answers1

2

I think better idea is to use databind method on your table. Check in debugger what is every rowData. Your problem could occur if one of your object in upper call is null. Check your call stack and check every object which is bind to your table because as I said it could be a problem. I could be also a problem with table inicialization. You can put your web code to question and designer especially table part.

ElConrado
  • 1,477
  • 4
  • 20
  • 46