2012-03-20 42 views
0
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錯誤

+0

空引用異常?從來沒有聽說過它...... – 2012-03-20 22:10:19

+2

在它裏面放置一個斷點,一步一步看看thmbNail的值,然後thmbNail的值[thmbNailCnt],試着理解什麼是null,用visual studio進行調試比嘗試用其他方法猜測哪裏更快錯誤是 – 2012-03-20 22:13:25

回答

3

你已經聲明瞭數組並且創建了它們,所以你的數組確實不是NRE的原因。但是,數組填滿了什麼?你在哪裏指定數組的內容有非空值?例如,你需要沿着線的東西:當你添加到末尾

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

也許你期待陣列自動展開。 C#不支持數組,所以你最好使用List<GroupBox>而不是GroupBox[]

+0

我想要做的是創建一個groupTextBox的數組。你能教我怎麼做嗎? – user1176111 2012-03-20 22:15:12

+0

@user,您已經創建了該數組。你沒有做的是實例化任何元素。換句話說,你的數組目前只是一個空值數組。你可以考慮在'newThumbNail'的開頭添加這行'thmbNail [thmbNailCnt] = new GroupBox();'。但那隻能解決第一個問題。你必須爲其他人做類似的事情。 – 2012-03-20 22:20:31

2

你聲明thmbNail並給它一個長度,但是你沒有填充它的任何元素。所以thmbNail[thmbNailCnt]返回一個空值,然後當您嘗試訪問其Visible屬性時,它會拋出NullReferenceException。也許你應該先分配一個新的值:

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

你會遇到與picBox和texBox數組相同的問題。還請記住在創建時將它們添加到您的表單中。

+0

我真的不喜歡在真實世界的應用程序中看到這樣的代碼。 thmbNail數組來自代碼之前寫了一些行,我們知道什麼是null或不是,所以*沒有理由*'ifs' – 2012-03-20 22:15:07

+0

沒關係,這對於學術界來說,他們的標準要低得多。在這裏插入皺眉表情。 – 2012-03-20 22:20:34

+0

我同意。最初,我以爲他試圖以不特定的順序填充數組中的條目。但第二次看代碼時,看起來更像是他只是簡單地添加一個新的縮略圖,所以應該使用列表來代替。 – 2012-03-20 22:21:08

1

您所做的只是聲明一組控件。

GroupBox[] thmbNail = new GroupBox[100]; 

要使用一個你需要實例化它,例如,

GroupBox[thmbnailcount] = new GroupBox(); 

相同的圖片框和文本框。

沒有什麼不同,以做

GroupBox myGroupBox; 

所有你要做的是告訴編譯器變量的類型。這不是一種價值類型,所以在你嘗試和使用它之前你必須得到一個新的。

相關問題