Craig Smith
Craig Smith

Reputation: 631

NullReferenceException sending input to a TextBox

I'm trying to send a string from form2 to form3 to pre-load the form with info. I call a method on form3 accepting the string which starts a linq query to set the controls on the form. I get a NullReferenceException was unhandled in the first foreach on recipeNameTextBox.Text = a.RecipeName; I can see that the query ran and the info is in a.RecipName. I think I might be getting this error because the control hasn't been drawn yet. Any Ideas how to get around this? Form2 code is here:

 private void updateButton_Click(object sender, EventArgs e)
 {
     Form3 Frm3 = new Form3();
     Frm3.Show();

     this.Hide();
     Frm3.takeInputFromForm2(recipeLabel.Text);
}

form3 code here:

 public void takeInputFromForm2(string incommingUpdateRecipe)
 {
     Query updateRecipe = new Query();
     IEnumerable<Recipe> newrecipe = updateRecipe.getRecipeInfo(incommingUpdateRecipe);
     foreach (var a in newrecipe)
     {
         recipeNameTextBox.Text = a.RecipeName;
         nationalityTextBox.Text = a.Nationality;
         eventTextBox.Text = a.Event;
         sourceTextBox.Text = a.Source;
         typeTextBox.Text = a.Type;
         ServingsTextBox.Text = a.Servings;
    }
    foreach (var b in newrecipe)
    {
        userRatingTextBox.Text = Convert.ToString(b.UserRating);
        familyRatingTextBox.Text = Convert.ToString(b.FamilyRating);
        healthRatingTextBox.Text = Convert.ToString(b.HealthRating);
        easeOfCookingTextBox.Text = Convert.ToString(b.CookingTime);
        cookingTimeTextBox.Text = Convert.ToString(b.CookingTime);
    }
    foreach (var c in newrecipe)
    {
        ingredientComboBox.Items.Add(c.RecipeIngredient);
    }
}

Upvotes: 2

Views: 2122

Answers (1)

Silvermind
Silvermind

Reputation: 5944

You are probably missing the InitializeComponent in your Form3 constructor.

Upvotes: 3

Related Questions