【问题标题】:C# Basic Syntax Error - No Idea What I've Done Wrong, But Probably Quite SimpleC# 基本语法错误 - 不知道我做错了什么,但可能很简单
【发布时间】:2011-08-03 10:56:58
【问题描述】:

想不出更好的标题。我只是想让有人告诉我这段代码有什么问题:

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:

【问题讨论】:

  • 自从我从事 WinForms 工作以来已经有很长时间了,但是在尝试在文本框上设置值之前,您不需要调用 InitializeComponent() 吗?如果我没记错的话,那就是构造控件的地方。

标签: c# syntax syntax-error


【解决方案1】:

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

这很可能是问题所在:

streetNameBox.Text = argstreet;

你已经在调用InitializeComponent()之前设置了,所以streetNameBox仍然是空的。

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

【讨论】:

  • 谢谢,不敢相信我没看到。
【解决方案2】:

您在分配给某些控件后初始化组件 - 我怀疑它们为空

在尝试使用任何控件之前必须先初始化组件

【讨论】:

    【解决方案3】:

    InitializeComponent 必须是第一行。

    这是因为它会创建您的所有 Windows 窗体组件。您试图在创建之前访问其中一个组件,并给出 NullReferenceException。

    【讨论】:

    • 已移除 - 我是说你先是如何回答的,但后来我发现你没有
    • P.S.我不敢相信我没有看到!
    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多