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

时间:2011-08-03 10:56:59

标签: 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;
        }

返回的错误是:

  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:

3 个答案:

答案 0 :(得分:6)

这不是语法错误,否则会是编译时失败。

这可能是问题所在:

streetNameBox.Text = argstreet;

您在调用InitializeComponent()之前设置了,因此streetNameBox仍为空。

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

答案 1 :(得分:3)

您在分配一些控件后初始化组件 - 我怀疑它们是空的

在尝试使用任何控件

之前,必须首先使用Initialize Component

答案 2 :(得分:2)

InitializeComponent需要是第一行。

这是因为它会创建所有Windows窗体组件。您正在尝试在创建组件之前访问其中一个组件,从而产生NullReferenceException。