【问题标题】:C# NullReferenceException ErrorC# NullReferenceException 错误
【发布时间】:2012-03-20 22:07:20
【问题描述】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Prototype
{
    public partial class Form1 : Form
    {
        object oDocument;
        int thmbNailCnt = 0;
        GroupBox[] thmbNail = new GroupBox[100];
        PictureBox[] picBox = new PictureBox[100];
        TextBox[] texBox = new TextBox[100];

        public Form1()
        {
            InitializeComponent();            
        }

        private void addWordToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void scheduleToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx)|*.doc;*.docx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;
                newThumbNail(1, sFileName);
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            oDocument = webBrowser1.Document;

        }

        private void button8_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.xls, *.xlsx)|*.xls;*.xlsx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.ppt, *.pptx)|*.ppt;*.pptx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.pdf)|*.pdf";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void newThumbNail(int docType, string fileName)
        {




            thmbNail[thmbNailCnt].Visible = true;            
            thmbNail[thmbNailCnt].Location = new Point(2, 5);
            thmbNail[thmbNailCnt].Size = new Size(222, 50);


            picBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
            picBox[thmbNailCnt].Visible = true;
            picBox[thmbNailCnt].Location = new Point(6, 13);
            picBox[thmbNailCnt].Size = new Size(31, 31);
            picBox[thmbNailCnt].Image = new Bitmap("images/excel.png");

            texBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
            texBox[thmbNailCnt].Visible = true;
            texBox[thmbNailCnt].Location = new Point(53, 24);
            texBox[thmbNailCnt].Size = new Size(163, 20);
            texBox[thmbNailCnt].Text = fileName;
            texBox[thmbNailCnt].Enabled = false;

            this.Controls.Add(thmbNail[thmbNailCnt]);
            this.Controls.Add(picBox[thmbNailCnt]);
            this.Controls.Add(texBox[thmbNailCnt]);


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }



    }
}

我的程序在进入函数 private void newThumbNail(int docType, string fileName) 时会抛出空引用错误。请帮忙。我相信我已经正确地声明了 groupBox、textBox 和 picture Box。但我不确定我是否声明了一个有效的 groupBox 数组。

【问题讨论】:

  • 空引用异常?没听说过...
  • 在里面放一个断点,单步查看thmbNail的值,然后看thmbNail[thmbNailCnt]的值,试着理解什么是null,用visual studio调试比尝试其他猜测哪里快错误是

标签: c# groupbox


【解决方案1】:

您已经声明并创建了数组,因此您的数组确实不是 NRE 的原因。但是,数组填充了什么?您在哪里指定数组的内容具有非空值?例如,您需要以下内容:

thmbNail[0] = new GroupBox(...)

您可能希望数组在添加到末尾时自动扩展。 C# 不支持数组,因此最好使用 List<GroupBox> 而不是 GroupBox[]

【讨论】:

  • 我想做的是创建一个 groupTextBox 数组。你能教我怎么做吗?
  • @user,您已经创建了数组。你没有做的是实例化任何元素。换句话说,您的数组目前只是一个空数组。您可以考虑将这一行 thmbNail[thmbNailCnt] = new GroupBox(); 添加到 newThumbNail 的开头。但这只会修复第一个数组。你必须为其他人做类似的事情。
【解决方案2】:

你声明了 thmbNail 并给它一个长度,但是你没有填充它的任何元素。所以thmbNail[thmbNailCnt] 返回一个空值,然后当您尝试访问它的Visible 属性时抛出 NullReferenceException。也许您应该先为其分配一个新值:

if (thmbNail[thmbNailCnt] == null)
    thmbNail[thmbNailCnt] = new GroupBox();
thmbNail[thmbNailCnt].Visible = true;

您将遇到与 picBox 和 texBox 数组相同的问题。还记得在创建时将它们添加到您的表单中。

【讨论】:

  • 我真的不想在现实世界的应用程序中看到这样的代码。 thmbNail 数组来自之前写的几行代码,我们知道什么是空或不空,所以 没有理由 for 'ifs'
  • 没关系,这是给学术界的,他们的标准要低得多。在此处插入皱眉表情。
  • 我同意。最初,我认为他试图以不特定的顺序填充数组中的条目。但是再看一遍代码,他看起来更像是想简单地添加一个新的缩略图,所以应该使用列表来代替。
【解决方案3】:

您所做的只是声明一个控件数组。

GroupBox[] thmbNail = new GroupBox[100];

要使用一个,您需要实例化它,例如

GroupBox[thmbnailcount] = new GroupBox();

图片框和文本框相同。

和做没有什么不同

GroupBox myGroupBox;

您所做的只是告诉编译器变量的类型。它不是一个值类型,所以在尝试使用它之前你必须在其中获取一个新的。

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多