C#NullReferenceException错误
每当我的程序进入函数private void newThumbNail(int docType,string fileName)时,它就会抛出一个空引用错误。请帮忙。我相信我已经正确地声明了groupBox、textBox和picture Box。但我不确定是否声明了groupBox的有效数组 您已经声明了数组并创建了它们,因此您的数组确实不是导致NRE的原因。但是,数组中填充了什么?在哪里指定数组的内容具有非空值?例如,您需要以下内容:C#NullReferenceException错误,c#,groupbox,C#,Groupbox,每当我的程序进入函数private void newThumbNail(int docType,string fileName)时,它就会抛出一个空引用错误。请帮忙。我相信我已经正确地声明了groupBox、textBox和picture Box。但我不确定是否声明了groupBox的有效数组 您已经声明了数组并创建了它们,因此您的数组确实不是导致NRE的原因。但是,数组中填充了什么?在哪里指定数组的内容具有非空值?例如,您需要以下内容: using System; using System.C
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)
{
}
}
}
可能您希望数组在添加到末尾时自动展开。C#不支持数组,因此最好使用
列表,而不是GroupBox[]
声明数组并创建它们,因此数组确实不是NRE的原因。但是,数组中填充了什么?在哪里指定数组的内容具有非空值?例如,您需要以下内容:
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)
{
}
}
}
可能您希望数组在添加到末尾时自动展开。C#不支持数组中的这一点,因此最好使用列表
而不是GroupBox[]
声明thmbNail并给它一个长度,但没有填充它的任何元素。因此thmbNail[thmbNailCnt]
返回一个空值,当您尝试访问它的Visible
属性时,它会抛出NullReferenceException。也许您应该首先为其指定一个新值:
thmbNail[0] = new GroupBox(...)
picBox和texBox阵列也会出现同样的问题。还记得在创建表单时将它们添加到表单中。声明thmbNail并为其指定长度,但尚未填充其任何元素。因此thmbNail[thmbNailCnt]
返回一个空值,当您尝试访问它的Visible
属性时,它会抛出NullReferenceException。也许您应该首先为其指定一个新值:
thmbNail[0] = new GroupBox(...)
picBox和texBox阵列也会出现同样的问题。还记得在创建表单时将它们添加到表单中。您所做的只是声明一个控件数组
if (thmbNail[thmbNailCnt] == null)
thmbNail[thmbNailCnt] = new GroupBox();
thmbNail[thmbNailCnt].Visible = true;
要使用它,您需要实例化它,例如
GroupBox[] thmbNail = new GroupBox[100];
picbox和文本框也是如此
这和做同样的事
GroupBox[thmbnailcount] = new GroupBox();
您所做的只是告诉编译器变量的类型。它不是一个值类型,因此在尝试使用它之前,必须在其中获取一个新的值。您所做的只是声明一个控件数组
if (thmbNail[thmbNailCnt] == null)
thmbNail[thmbNailCnt] = new GroupBox();
thmbNail[thmbNailCnt].Visible = true;
要使用它,您需要实例化它,例如
GroupBox[] thmbNail = new GroupBox[100];
picbox和文本框也是如此
这和做同样的事
GroupBox[thmbnailcount] = new GroupBox();
您所做的只是告诉编译器变量的类型。它不是一个值类型,所以在尝试使用它之前,必须在其中获取一个新的值。null引用异常?从未听说过它…在其中放置一个断点,单步查看thmbNail的值,然后查看thmbNail[thmbNailCnt]的值,尝试了解什么是null,使用visual studio进行调试比尝试其他方法更快猜测错误在哪里是null引用异常?从未听说过它…在它里面放置一个断点,单步查看thmbNail的值,然后查看thmbNail[thmbNailCnt]的值,尝试了解什么是空值,使用visual studio进行调试比尝试其他方法更快猜测错误的位置我真的更喜欢在真实世界的应用程序中看不到这样的代码。thmbNail数组来自于以前编写的代码,我们知道什么是空的,所以没有理由使用“ifs”这个词,这是学术界的,他们的标准要低得多。在这里插入皱眉表情。我同意。起初,我认为他是在尝试以不特定的顺序填充数组中的条目。但再看一下代码,他更想简单地添加一个新的缩略图,所以应该使用列表。我真的不希望在现实世界的应用程序中看到这样的代码。thmbNail数组来自于以前编写的代码,我们知道什么是空的,所以没有理由使用“ifs”这个词,这是学术界的,他们的标准要低得多。在这里插入皱眉表情。我同意。起初,我认为他是在尝试以不特定的顺序填充数组中的条目。但是再看一下代码,看起来他更想简单地添加一个新的缩略图,所以应该使用列表来代替。我想做的是创建一个groupTextBox数组。你能教我怎么做吗?@user,你已经创建了数组。您没有做的是实例化任何元素。换句话说,您的数组目前只是一个空数组。您可以考虑添加此行<代码> thbPIn[thbnaNcNt]=新GROMPBOX();到新缩略图的开头
。但这只会修复第一个阿拉伯半岛。你必须为其他人做类似的事情。我想做的是创建一个groupTextBox数组。你能教我怎么做吗?@user,你已经创建了数组。您没有做的是实例化任何元素。换句话说,您的数组目前只是一个空数组。您可以考虑添加此行<代码> thbPIn[thbnaNcNt]=新GROMPBOX();到新缩略图的开头
。但这只会修复第一个阿拉伯半岛。你必须为其他人做类似的事情。