0

I am working on a console based app that will email the user of the app an email with a description of a problem with clients trying to use a mobile app. What I am currently attempting right now is setting up the emailing piece of the code. The only problem I am having is when I try to run it, it keeps telling me that I have a null reference exception. Below is the code I am trying to work with. What it is supposed to do is loop through all of the transactions in this list, if it was fixed then nothing will really happen. Otherwise it will take the transaction, match it to user information they have on a website and then puts in all in a string so we can put it in a email.

The problem is on the line with myHomeBody = matchRecords.ToString(); When I went through it in debugging, I got the value I needed in here DashSupportMonitorBO.TransactionInformation.UserName.get returned, but in matchRecords where I want that value, it is coming up as null.

So is there a way I can just get that value and move it into match records, or is there just a simple way of fixing this that I am just completely missing?

foreach (TransactionInformation transaction in recentFailedTransactions)
{
     string displayErrorBody;
     string myHomeBody;
     if (TransactionSuccessFound(transaction))
     {
         BOAssistant.WriteLine("Transaction fixed");
     }
     else
     {
         displayErrorBody = transaction.DisplayException.ToString();
         MyHomeInformation matchRecords = GetUserDataFromMyHome(transaction.UserName);   
         myHomeBody = matchRecords.ToString();
         string emailBody = "Here is the displayErrorBody: \n" + displayErrorBody + "Here is all the user information regarding the error: \n" + myHomeBody;
         BOAssistant.WriteLine(emailBody);
         SendEmail(emailBody, "Errors in Dash Mobile");
     }
 }

Here is the code for GetUserDataFromMyHome

 private static MyHomeInformation GetUserDataFromMyHome(string username)
    {
        MyHomeInformation myHomeInformation = null;
        using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.MyHomeConnectionString))
        {
            SqlCommand sqlError = connection.CreateCommand();
            sqlError.CommandText = @"SELECT USER_NAME, EMAIL, FIRST_NAME, LAST_NAME, TRAVELER_UID FROM [USER] WHERE USER_NAME = @USER_NAME";
            sqlError.Parameters.Add("@USER_NAME", System.Data.SqlDbType.VarChar).Value = username;
            connection.Open();
            SqlDataReader reader = sqlError.ExecuteReader();

            if(reader.Read())
            {
                myHomeInformation = new MyHomeInformation();
                myHomeInformation.myHomeUserName = Utilities.FromDBValue<string>(reader["USER_NAME"]);
                myHomeInformation.myHomeEmail = Utilities.FromDBValue<string>(reader["EMAIL"]);
                myHomeInformation.myHomeFirstName = Utilities.FromDBValue<string>(reader["FIRST_NAME"]);
                myHomeInformation.myHomeLastName = Utilities.FromDBValue<string>(reader["LAST_NAME"]);
                myHomeInformation.myHomeTravelerUID = (Utilities.FromDBValue<Guid>(reader["TRAVELER_UID"])).ToString();
            }
        }
        return myHomeInformation;
    }
user4970927
  • 179
  • 7

2 Answers2

0

GetUserDataFromMyHome is returning null, not transaction.UserName. Perhaps that means the data wasn't found or there was some other error.

This means that matchRecords will be assigned null, and you can't call ToString on null. If it's ok for GetUserDataFromMyHome to return null, just make sure you handle the null result properly - if not, find out why it is returning null.

Luaan
  • 57,516
  • 7
  • 84
  • 100
0

I cant add a comment due to not having 50 points yet, but your comment suggesting it works, shows you calling a different method. The Error states that when you call GetUserDataFromMyHome it doesn't return anything so then fails on the line you are talking about. When you debug, you need to ensure transaction.UserName being passed into your method has a value and perhaps show the code in the method since this is the issue. In both cases before you try to set myHomeBody you should perform some defensive coding and first check that matchRecords is not null before attempting to use it.

bilpor
  • 2,357
  • 5
  • 21
  • 55
  • I have just seen your additional post. The fact that in this method the first thing you do is initialise the return value to Null, means that you should most definitely test for null first on return before attempting to use the object. You need to debug this method. – bilpor Aug 10 '15 at 16:06