C# 调用对象数组时出现空引用异常
我早些时候试图寻求一些帮助,但我认为我没有提供足够的信息,尽管我很感激所有的建议 目标只是将对象房间的新实例添加到数组中,并打印到列表框中。当用户尝试输入已存在的房间名称时,它只应显示在阵列中已存在房间的规格中 我一直得到一个空引用异常 这是我的密码:C# 调用对象数组时出现空引用异常,c#,nullreferenceexception,C#,Nullreferenceexception,我早些时候试图寻求一些帮助,但我认为我没有提供足够的信息,尽管我很感激所有的建议 目标只是将对象房间的新实例添加到数组中,并打印到列表框中。当用户尝试输入已存在的房间名称时,它只应显示在阵列中已存在房间的规格中 我一直得到一个空引用异常 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间
{
公共部分类Form1:Form
{
房间[]房间阵列=新房间[20];
整数计数=0;
公共表格1()
{
初始化组件();
}
私有无效btnAddRm_单击(对象发送方,事件参数e)
{
双倍长、宽、高;
如果(VerifyRoomName()==true)
{
如果(txtRmLen.Text==“”| | txtRmWid.Text==“”| | txtRmHt.Text==“”)
{
for(int i=0;i
您的代码应该是
if (roomArray[i] != null)
无论何时创建数组,都必须先初始化它的各个项,然后才能访问它们
Room[] roomArray = new Room[20];
roomArray[0] = new Room();
因为Room[]中的Room元素没有初始化 试一试
public Form1()
{
初始化组件();
对于(int i=0;i
正如其他答案所说,您需要在开始使用阵列之前对其进行初始化。当你写作时:
Room[] roomArray = new Room[20];
您告诉计算机要做的是为您保留足够的内存,以便引用类型为房间的20个对象。建议的其他解决方案也不错,但如果您想要提高性能,请尝试以下方法:
根据SO的回答,使用以下功能将比目前提供的其他解决方案更有效。这也有来自中国的支持证据
注意:我已转换为使用泛型
//
///用默认值填充数组
///
///
///要用默认值填充的数组
///默认值
公共静态void MemSet(T[]数组,T值)
{
if(数组==null)
{
抛出新的ArgumentNullException(“数组”);
}
int block=32,index=0;
int length=Math.Min(块、数组、长度);
//填充初始数组
while(索引<长度)
{
数组[index++]=值;
}
长度=数组长度;
while(索引<长度)
{
BlockCopy(数组,0,数组,索引,Math.Min(块,长度-索引));
索引+=块;
区块*=2;
}
}
用法
Memset<Room>(roomArray, new Room());
Memset(roomArray,新Ro
/// <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());