2

form2 から form3 に文字列を送信して、フォームに情報をプリロードしようとしています。フォームにコントロールを設定するための linq クエリを開始する文字列を受け入れる form3 でメソッドを呼び出します。私は、recipeNameTextBox.Text = a.RecipeName; の最初の foreach で NullReferenceException was unhandled を取得します。クエリが実行され、情報が a.RecipName にあることがわかります。コントロールがまだ描画されていないため、このエラーが発生する可能性があると思います。これを回避する方法はありますか?Form2 コードは次のとおりです。

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

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

ここにform3コード:

 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);
    }
}
4

1 に答える 1

3

おそらくInitializeComponent、Form3 コンストラクターに がありません。

于 2012-10-21T23:04:35.117 に答える