2013-08-25 25 views
1

我創建了一個表格,將作爲的InputBox的InputBox的NullReferenceException

public partial class InputBox : Form 
    { 
     public string sample 
     { 
      get 
      { return TextBox1.Text; } 
      set 
      { TextBox1.Text = value; } 
     } 

     public InputBox(string title, string question) 
     { 
      this.Text = title; 
      Label1.Text = question; 
      InitializeComponent(); 
     } 
    } 

在我的其他形式我把這種形式:

private void Button1_Click(object sender, EventArgs e) 
    { 
     InputBox dlg = new InputBox("TITLE", "Sample Question ?"); 
     dlg.ShowDialog(); 

     if (dlg.DialogResult == DialogResult.OK) 
     { 
      TextBox1.Text = dlg.sample; 
     } 
    } 

爲什麼我的NullReferenceException? Object reference not set to an instance of an object.

+0

的可能的複製[什麼是一個NullReferenceException,如何解決呢?(HTTP:// stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine

回答

3

做如下

public InputBox(string title, string question) 
{ 
    InitializeComponent(); 
    this.Text = title; 
    Label1.Text = question; 
} 

首先初始化組件,之後你可以設置屬性