0

申し訳ありませんが、かなり基本的なことはわかっていますが、手が届きません。助けてください、どうもありがとう

Winforms Null 参照例外が処理されませんでした - オブジェクト参照がオブジェクトのインスタンスに設定されていません。

Model.ClientModel Client = new Model.ClientModel(); // Create Instance of Model
Client.id = int.Parse(row.Cells[0].Value.ToString()); // Setting fields
Client.clientName = row.Cells[1].Value.ToString();// Setting fields
Client.company = row.Cells[2].Value.ToString();
Client.website = row.Cells[3].Value.ToString();
Client.emails = row.Cells[4].Value.ToString();
Client.phone= row.Cells[5].Value.ToString();
Client.mobile = row.Cells[6].Value.ToString();
Client.location = row.Cells[7].Value.ToString();
Client.referrer = row.Cells[8].Value.ToString();
Client.currency = row.Cells[9].Value.ToString();
Client.ratePerWord = row.Cells[10].Value.ToString();
Client.notes = row.Cells[11].Value.ToString();

Clients.EditClient editClient = new Clients.EditClient(Client); // Passing Model to the constructor of EditClient Class

editClient.Show(); // Displaying form

EditClient (フォームクラス):

public partial class EditClient : Form
{
   public Model.ClientModel Client;

   public EditClient(Model.ClientModel Client)
   {
      // TODO: Complete member initialization
      this.Client = Client; //Getting the Model Object passed by the previous class
      fillForm();
      InitializeComponent();
   }

   public void fillForm()
   {
      //Here I set all the model data to the fields

      textBox1.Text = Client.clientName;  //This is where the ERROR Occurs - NULLReferenceException was unhandled.  Object reference not set to an instance of an object.
      textBox2.Text = Client.company;
      textBox3.Text = Client.website;
      textBox4.Text = Client.emails;
      textBox5.Text = Client.phone;
      textBox6.Text = Client.mobile;
      textBox7.Text = Client.location;
      textBox8.Text = Client.referrer;
      textBox9.Text = Client.currency;
      textBox10.Text = Client.ratePerWord;
      textBox11.Text = Client.notes;
   }
}

何が原因なのか教えてください。いくつかの方法を試しましたが、同じエラーが発生します。

4

2 に答える 2

2

あなたの問題は、フォームが初期化される前にフォームに入力しようとしているため、すべてのテキストボックスがnullであるため、代わりにEditClientコンストラクターでこれを試してください:

public EditClient(Model.ClientModel Client)
{
    // TODO: Complete member initialization
    this.Client = Client; //Getting the Model Object passed by the previous class
    InitializeComponent();
    fillForm();        
}
于 2013-09-19T17:58:09.660 に答える