2013-02-27 72 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 Room_Class 
{ 
    public partial class Form1 : Form 
    { 
     Room[] roomArray = new Room[20]; 
     int count = 0; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnAddRm_Click(object sender, EventArgs e) 
     { 
      double length, width, height; 
      if (VerifyRoomName() == true) 
      { 


       if (txtRmLen.Text == "" || txtRmWid.Text == "" || txtRmHt.Text == "") 
       { 
        for (int i = 0; i < roomArray.Length; i++) 
        { 
         if (txtRmName.Text == roomArray[i].getRoomName()) 
         { 
          txtRmName.Text = roomArray[i].getRoomName(); 
          txtRmLen.Text = roomArray[i].getLength().ToString(); 
          txtRmHt.Text = roomArray[i].getCeilingHeight().ToString(); 
          txtRmWid.Text = roomArray[i].getWidth().ToString(); 
         } 
         else 
         { 
          roomArray[count] = new Room(roomName); 
          count++; 
         } 
        } 
       } 
       else 
       { 
        try 
        { 
         length = double.Parse(txtRmLen.Text); 
         width = double.Parse(txtRmWid.Text); 
         height = double.Parse(txtRmHt.Text); 
         for (int i = 0; i < roomArray.Length; i++) 
         { 
          if (txtRmName.Text == roomArray[i].getRoomName()) 
          { 
           txtRmName.Text = roomArray[i].getRoomName(); 
           txtRmLen.Text = roomArray[i].getLength().ToString(); 
           txtRmHt.Text = roomArray[i].getCeilingHeight().ToString(); 
           txtRmWid.Text = roomArray[i].getWidth().ToString(); 
          } 
          else 
          { 
           roomArray[count] = new Room(roomName, length, width, height); 
           count++; 
          } 
         } 
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.Message + "\n\nPlease Enter Valid Values", "Error!"); 
        } 
       } 
       PrintList(); 
      } 

     } 


     private void PrintList() 
     { 
      double paintTotal = 0, feetTotal = 0; 
      string RoomName; 
      lstRoomList.Items.Clear(); 
      for (int i = 0; i < count; i++) 
      { 
       RoomName = roomArray[i].getRoomName() + "\t" + roomArray[i].SquareFeet().ToString("n1") + "\t" + roomArray[i].GallonsPaint().ToString("n1"); 
       lstRoomList.Items.Add(RoomName); 
       paintTotal += roomArray[i].GallonsPaint(); 
       feetTotal += roomArray[i].SquareFeet(); 
       lblTtlGallons.Text = paintTotal.ToString("n1"); 
       lblTtlSqFt.Text = feetTotal.ToString("n1"); 
      } 
     } 

     private bool VerifyRoomName() 
     { 
      if (roomName == "") 
      { 
       MessageBox.Show("Please Enter a Room Name", "Error!"); 
       return false; 
      } 
      else 
       return true; 
     } 
    } 
} 
+0

什麼是ROOMNAME – 2013-02-27 02:07:40

+0

哪裏是例外? – John3136 2013-02-27 02:08:02

+2

歡迎來到Stack Overflow!幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 – 2013-02-27 02:08:20

回答

2

你的代碼應該是

if (roomArray[i] != null) 

當你創建一個數組,你必須初始化它的單個項目,然後才能訪問它們。

Room[] roomArray = new Room[20]; 

roomArray[0] = new Room(); 
+0

即將給出相同的答案,但隨後發現OP正在初始化'roomArray' in this part'roomArray [count] = new Room(roomName);'//儘管在其他部分中有可疑 – exexzian 2013-02-27 02:13:49

+0

@Bingo Yeah我也看到了......我只是認爲他最大的問題可能是理解如何使用數組,因此我的回答:) – scartag 2013-02-27 02:15:10

3

因爲房間內的房間元素[]我們沒有初始化。

嘗試

public Form1() 
{ 
    InitializeComponent(); 
    for(int i = 0; i < roomyArray.Length; i++) roomArray[i] = new Room(); 
} 
0

至於其他的答案說,你需要在開始使用它之前初始化數組。當你寫:

Room[] roomArray = new Room[20]; 

你告訴計算機做的就是儲備你足夠的內存類型Room的20個對象。提出的其他解決方案都很好,但如果您需要性能,請嘗試以下操作:

根據this SO答案,使用以下函數比迄今提供的其他解決方案更具性能。這從this blog post.

注也佐證:我已經轉換到使用泛型

/// <summary> 
    /// Fills an array with a default value 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="array">The array to fill with a default value</param> 
    /// <param name="value">The default value</param> 
    public static void MemSet<T>(T[] array, T value) 
    { 
     if (array == null) 
     { 
      throw new ArgumentNullException("array"); 
     } 

     int block = 32, index = 0; 
     int length = Math.Min(block, array.Length); 

     //Fill the initial array 
     while (index < length) 
     { 
      array[index++] = value; 
     } 

     length = array.Length; 
     while (index < length) 
     { 
      Buffer.BlockCopy(array, 0, array, index, Math.Min(block, length - index)); 
      index += block; 
      block *= 2; 
     } 
    } 

使用

Memset<Room>(roomArray, new Room());