简体   繁体   English

C#NullReferenceException错误

[英]C# NullReferenceException Error

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)
        {

        }



    }
}

My program throws a null reference error whenever it enters the function private void newThumbNail(int docType, string fileName). 每当进入函数private void newThumbNail(int docType,string fileName)时,我的程序都会引发空引用错误。 Please Help. 请帮忙。 I believe I have declared the groupBox, textBox, and picture Box properly. 我相信我已经正确声明了groupBox,textBox和picture Box。 But I'm not sure if I have declared a valid array of groupBox. 但是我不确定是否声明了有效的groupBox数组。

You've declared arrays and created them, so your arrays are indeed not the cause of the NRE. 您已经声明了数组并创建了它们,因此您的数组确实不是NRE的原因。 However, what are the arrays filled with? 但是,数组填充了什么? Where do you specify that the contents of the arrays have non-null values? 您在哪里指定数组的内容具有非null值? For example, you need something along the lines of: 例如,您需要遵循以下要求:

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

Possibly you are expecting arrays to auto expand when you add to the end. 添加到最后时,可能期望数组自动扩展。 C# doesn't support that with arrays, so you'd be better off using a List<GroupBox> instead of a GroupBox[] . C#不支持使用数组,因此最好使用List<GroupBox>而不是GroupBox[]

You declare thmbNail and give it a length, but you have not populated any of its elements. 您声明了thmbNail并为其指定了一个长度,但尚未填充其任何元素。 So thmbNail[thmbNailCnt] returns a null value, which then throws the NullReferenceException when you try to access its Visible property. 因此, thmbNail[thmbNailCnt]返回一个空值,当您尝试访问其Visible属性时,它将引发NullReferenceException。 Perhaps you should assign a new value into it first: 也许您应该首先为其分配一个新值:

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

You'll have the same issue with the picBox and texBox arrays. picBox和texBox数组会遇到相同的问题。 Also remember to add them to your form when created. 还记得创建时将它们添加到表单中。

All you've done is declare an array of controls. 您所要做的就是声明一个控件数组。

GroupBox[] thmbNail = new GroupBox[100];

To use one you need to instantiate it eg 要使用一个,您需要实例化它,例如

GroupBox[thmbnailcount] = new GroupBox();

Same for picbox and text box. 同样适用于picbox和文本框。

No different to doing 没什么不同

GroupBox myGroupBox;

All you've done is tell the compiler the type of variable. 您要做的只是告诉编译器变量的类型。 It's not a value type so you have to get a new in there before you try and use it. 它不是值类型,因此在尝试使用它之前,您必须先获取一个新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM