【问题标题】:c# Windows forms 'Object reference not set to an instance of an object'c#Windows窗体'对象引用未设置为对象的实例'
【发布时间】:2015-03-27 14:47:08
【问题描述】:

我来问是因为我是使用 Windows 窗体的新手,我需要一些帮助。

我有一个包含 2 个表单的 Windows 窗体项目。 MainForm 和 InnerForm。

我正在尝试从 InnerForm 访问 MainForm 中的 TableLayoutPanel,以在此 tablelayoutpanel 中添加新行,并在 InnerForm 中发生一些操作。

我有下一个代码:

主窗体:

 public partial class MainForm : Form
 {
    public MainForm()
    {
        InitializeComponent();

        TableLayoutPanel panel = tableLayoutPanel1;

        panel.ColumnCount = 4;
        panel.RowCount = 1;
        panel.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
        panel.Controls.Add(new Label() { Text = "Tag/ID" }, 0, 0);
        panel.Controls.Add(new Label() { Text = "Tipo" }, 1, 0);
        panel.Controls.Add(new Label() { Text = "Acción" }, 2, 0);
        panel.Controls.Add(new Label() { Text = "Ejecutar" }, 3, 0);
    }

    private void AddInnerForm(string url)
    {
        var inner = new InnerForm(url)
        // more code
    }

    public void agregarRow(ConsoleMessageEvents args){
        // some action with tableLayoutPanel1
    }
 }

内窗体:

 public partial class InnerForm : UserControl
 {
    MainForm theMain;

    public InnerForm(MainForm main)
    {
        theMain = main;
    }

    public InnerForm(string url)
    {
        InitializeComponent();
        // more code
    }

    private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
    {
       theMain.agregaRow(args);
    }
 }

但是当我调试程序时,我得到了这个错误:

An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe

Additional information: Object reference not set to an instance of an object.

InnerForm 的这一行:

private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
   **theMain.agregaRow(args)**;
}

这里有什么问题?

【问题讨论】:

标签: c# .net winforms


【解决方案1】:

这应该可以解决它。 theMain 字段未初始化,您还需要在每个构造函数中调用 InitializeComponenets 来创建子控件。

   public partial class InnerForm : UserControl
 {
    MainForm theMain;

    public InnerForm(MainForm main)
    {
        InitializeComponent();
        theMain = main;
    }

    public InnerForm(string url, MainForm mainForm)
    {
        this.theMain = mainForm;
        InitializeComponent();
        // more code
    }

    private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
    {
       theMain.agregaRow(args);
    }
 }

在 MainForm 中:

private void AddInnerForm(string url)
    {
        var inner = new InnerForm(url, this)
        // more code
    }

【讨论】:

    【解决方案2】:

    从您的代码中很容易看到您在此构造函数中初始化 theMain 变量:

    public InnerForm(MainForm main)
    {
        theMain = main;
    }
    

    但是在您的代码var inner = new InnerForm(url) 中,您使用的是 another 构造函数,该构造函数未初始化此变量 - 所以 theMain 留下了 null 并且您在尝试访问时遇到了该异常它的方法。

    public InnerForm(string url)
    {
        InitializeComponent();
        // more code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多