简体   繁体   中英

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). Please Help. I believe I have declared the groupBox, textBox, and picture Box properly. But I'm not sure if I have declared a valid array of groupBox.

You've declared arrays and created them, so your arrays are indeed not the cause of the NRE. However, what are the arrays filled with? Where do you specify that the contents of the arrays have non-null values? 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[] .

You declare thmbNail and give it a length, but you have not populated any of its elements. So thmbNail[thmbNailCnt] returns a null value, which then throws the NullReferenceException when you try to access its Visible property. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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