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數組。C#NullReferenceException錯誤
空引用異常?從來沒有聽說過它...... – 2012-03-20 22:10:19
在它裏面放置一個斷點,一步一步看看thmbNail的值,然後thmbNail的值[thmbNailCnt],試着理解什麼是null,用visual studio進行調試比嘗試用其他方法猜測哪裏更快錯誤是 – 2012-03-20 22:13:25