How to decouple "assembly" from creating and adding a new instance of "lookaheadRunInfo",

I'm trying to "collect" GetString (2) until GetString (0) changes, so I'm trying to figure out how to separate the "collection" from creating and adding a new instance of "lookaheadRunInfo"? as below which throws an exception System.NullReferenceException was unhandled by user code

on the line lookaheadRunInfo.gerrits.Add(rdr.GetString(1));

, can anyone provide any advice on how to fix this issue?

       try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();

            string sql = @"select lr.ec_job_link, cl.change_list ,lr.submitted_by, lr.submission_time,lr.lookahead_run_status
                                from lookahead_run as lr, lookahead_run_change_list as lrcl, change_list_details as cld,change_lists as cl
                                where lr.lookahead_run_status is null
                                and lr.submission_time is not null
                                and lrcl.lookahead_run_id = lr.lookahead_run_id
                                and cl.change_list_id = lrcl.change_list_id
                                and cl.change_list_id not in (select clcl.change_list_id from component_labels_change_lists as clcl)
                                and cld.change_list_id = lrcl.change_list_id
                                group by lr.lookahead_run_id, cl.change_list
                                order by lr.submission_time desc
                                limit 1000
                                ";

            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader rdr = cmd.ExecuteReader();
            var ECJoblink_previous ="";
            var gerritList = new List<String>();

            while (rdr.Read())
            {
                //Console.WriteLine(rdr[0] + " -- " + rdr[1]);
                //Console.ReadLine();
                var lookaheadRunInfo = new LookaheadRunInfo();
                lookaheadRunInfo.ECJobLink = rdr.GetString(0);
                if (ECJoblink_previous == lookaheadRunInfo.ECJobLink)
                {
                    //Keep appending the list of gerrits until we get a new lookaheadRunInfo.ECJobLink
                    lookaheadRunInfo.gerrits.Add(rdr.GetString(1));
                }
                else
                {
                    lookaheadRunInfo.gerrits = new List<string> { rdr.GetString(1) };
                }
                ECJoblink_previous = lookaheadRunInfo.ECJobLink;
                lookaheadRunInfo.UserSubmitted = rdr.GetString(2);
                lookaheadRunInfo.SubmittedTime = rdr.GetString(3).ToString();
                lookaheadRunInfo.RunStatus = "null";
                lookaheadRunInfo.ElapsedTime = (DateTime.UtcNow-rdr.GetDateTime(3)).ToString();
                lookaheadRunsInfo.Add(lookaheadRunInfo);                  
            }

            rdr.Close();
        }
        catch
        {
            throw;
        }

      

+3


source to share


1 answer


If I understand your requirements correctly, you want to keep one lookaheadRunInfo

for multiple rows of the result set until GetString (0) changes. Is it correct?

In this case, you have some important logic problems. As it is written, even if we fix the null reference, you will get a new one lookaheadRunInfo

with every line.

Try the following:



string ECJoblink_previous = null;
LookAheadRunInfo lookaheadRunInfo = null;
while (rdr.Read())
{
    if (ECJoblink_previous != rdr.GetString(0))  //A new set of rows is starting
    {
        if (lookaheadRunInfo != null)
        {
            lookaheadRunsInfo.Add(lookaheadRunInfo); //Save the old group, if it exists
        }
        lookaheadRunInfo = new LookAheadRunInfo     //Start a new group and initialize it
        {
            ECJobLink = rdr.GetString(0),
            gerrits = new List<string>(),
            UserSubmitted = rdr.GetString(2),
            SubmittedTime = rdr.GetString(3).ToString(),
            RunStatus = "null",
            ElapsedTime = (DateTime.UtcNow-rdr.GetDateTime(3)).ToString()
        }
    }
    lookahead.gerrits.Add(rdr.GetString(1));   //Add current row
    ECJoblink_previous = rdr.GetString(0);     //Keep track of column 0 for next iteration
}
if (lookaheadRunInfo != null)
{
    lookaheadRunsInfo.Add(lookaheadRunInfo); //Save the last group, if there is one
}

      

The idea is here:

  • Start from scratch, nothing initialized
  • Monitor column 0. When it changes (as in the first line), save the old list and start a new one
  • Add to current list with each iteration
  • When finished, save the rest of the items to your list. A null check is required if the reader returned 0 lines.
+1


source







All Articles