C#基本语法错误-不知道我';我做错了,但可能很简单
想不出更好的标题了。我只想有人告诉我这个代码有什么问题:C#基本语法错误-不知道我';我做错了,但可能很简单,c#,syntax,syntax-error,C#,Syntax,Syntax Error,想不出更好的标题了。我只想有人告诉我这个代码有什么问题: public AddressChooser(string argstreet, string argsuburb, string argcountry, string argstate, string argunit, int argstreetNumber, int argpostCode) { streetNameBox.Text = argstreet; suburbB
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:
这不是语法错误,否则将是编译时失败 这可能就是问题所在:
streetNameBox.Text = argstreet;
在调用InitializeComponent()
之前,您已经设置了该值,因此streetNameBox
仍将为空
您可能应该调用
InitializeComponent
,以便在执行其余构造之前初始化所有与设计器相关的字段。InitializeComponent需要是第一行
这是因为它创建了所有Windows窗体组件。您试图在创建某个组件之前访问该组件,导致出现NullReferenceException。您在分配给某些控件后初始化了该组件-我怀疑它们为null
在尝试使用任何控件之前,必须先初始化组件我已经很久没有使用WinForms了,但是在尝试设置文本框上的值之前,您不需要调用InitializeComponent()吗?如果我没记错的话,那就是控件被构造的地方。移除-我是说你首先是如何回答的,但后来我发现你没有tP.s。真不敢相信我居然没看到!谢谢,真不敢相信我没看到。