C#基本语法错误 - 不知道我做错了什么,但可能很简单

Yos*_*ter -1 c# syntax syntax-error

想不出更好的头衔.我只想让别人告诉我这段代码有什么问题:

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;
        }
Run Code Online (Sandbox Code Playgroud)

返回的错误是:

  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:
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 6

这不是语法错误,否则它将是编译时失败.

这可能是问题所在:

streetNameBox.Text = argstreet;
Run Code Online (Sandbox Code Playgroud)

调用之前设置了它InitializeComponent(),所以streetNameBox仍然是null.

您可能应该调用,InitializeComponent以便在执行其余构造之前初始化所有与设计器相关的字段.