2011-08-03 52 views
-1

想不到一個更好的標題。我只是希望有人讓我知道什麼是錯,此代碼:C#基本語法錯誤 - 不知道我做了什麼錯誤,但可能很簡單

public AddressChooser(string argstreet, string argsuburb, string argcountry, string argstate, string argunit, int argstreetNumber, int argpostCode) 
     { 
      streetNameBox.Text = argstreet; 
      suburbBox.Text = argsuburb; 
      countryBox.Text = argcountry; 
      stateBox.Text = argstate; 
      unitBox.Text = argunit; 
      streetNumberBox.Value = argstreetNumber; 
      postCodeBox.Value = argpostCode; 
      InitializeComponent(); 
      cancel.DialogResult = DialogResult.Cancel; 
      save.DialogResult = DialogResult.OK; 
     } 

返回的錯誤是:

System.NullReferenceException was unhandled 
    Message=Object reference not set to an instance of an object. 
    Source=MultipleForms 
    StackTrace: 
     at MultipleForms.AddressChooser..ctor(String argstreet, String argsuburb, String argcountry, String argstate, String argunit, Int32 argstreetNumber, Int32 argpostCode) in C:\Users\Yoshie\Local Settings\Documents\Visual Studio 2010\Projects\MultipleForms\MultipleForms\Address Selector.cs:line 17 
     at MultipleForms.Form1.changeAddress_Click(Object sender, EventArgs e) in C:\Users\Yoshie\Local Settings\Documents\Visual Studio 2010\Projects\MultipleForms\MultipleForms\MultiForm Example.cs:line 23 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at MultipleForms.Program.Main() in C:\Users\Yoshie\Local Settings\Documents\Visual Studio 2010\Projects\MultipleForms\MultipleForms\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+1

它已經我已經好久沒上的WinForms工作的任何控件之前,但在嘗試設置文本框的值之前,你不需要調用InitializeComponent()嗎?如果我沒有記錯的話,那就是控件的構建。 – Ragesh

回答

6

這不是一個語法錯誤,否則將是一個編譯時失敗。

這很可能是這個問題:

streetNameBox.Text = argstreet; 

你已經設置調用InitializeComponent()之前,所以streetNameBox將仍然爲空。

您應該致電InitializeComponent,以便所有與設計師有關的字段都可以在執行其餘結構之前初始化。

+0

謝謝,不敢相信我沒有看到。 – YoshieMaster

2

InitializeComponent需要成爲第一行。

這是因爲它創建了所有的Windows窗體組件。您正試圖在創建組件之前訪問其中一個組件,並提供NullReferenceException。

+0

*刪除* - 我在說你是如何回答第一,但後來我看到你沒有 – YoshieMaster

+0

P.S.我不敢相信我沒有看到! – YoshieMaster

3

你必須分配給一些控件後初始化組件 - 我懷疑他們是空

初始化組件必須先來嘗試使用

相關問題