尝试创建类数组列表时出现NullReferenceException

时间:2014-10-08 07:18:05

标签: c# asp.net nullreferenceexception

我在SQL中有一个包含不同服务的表。每个服务都有一些特性(它意味着每一行,在SQL中有不同的列)。例如:

s1:id1,a1,b1(id:int,a:smallint,b:real)

s2:id2,a2,b2

...

我希望在c#中列出这些服务。

由于某些原因,每列都应该有一些属性。例如:

对于a1表单s1,我们应该有:

a1.type,a1.typical,a1.min

这就是我所做的:

public class ClsAdvertisement
{
public string type { get; set; }
public double typical { get; set; }
public double min { get; set; }
}

protected void MyFuction()
{
        List<ClsAdvertisement[]> services = new List<ClsAdvertisement[]>();
        SqlDataReader dr1 = cmd1.ExecuteReader();
        if (dr1.HasRows)
        {
        while (dr1.Read())
        {
            ClsAdvertisement[] qualities=new ClsAdvertisement[3];

           if (dr1.IsDBNull(dr1.GetOrdinal("id")) == false)
           { 
              qualities[0].typical = double.Parse(dr1["id"].ToString());
              qualities[0].type = "id";
              qualities[0].min = qualities[0].typical;
           }
           if (dr1.IsDBNull(dr1.GetOrdinal("a")) == false)
           {
               qualities[1].typical = double.Parse(dr1["a"].ToString());
               qualities[1].type = "a";
               qualities[1].min = qualities[1].typical - 1.0;
           }
           if (dr1.IsDBNull(dr1.GetOrdinal("b")) == false)
           {
               qualities[2].typical = double.Parse(dr1["b"].ToString());
               qualities[2].type = "b";
               qualities[2].min = qualities[2].typical - 1.0;
           }

           services.Add(new ClsAdvertisement[] { qualities[0], qualities[1], qualities[2] });
        }
    }
    if (dr1.IsClosed == false) dr1.Close();
}

这是一个很好的方法吗?

如果,是的,我收到错误

  

System.NullReferenceException:未将对象引用设置为对象的实例

在线:

qualities[0].typical = double.Parse(dr1["id"].ToString());

从我的搜索中,我知道原因是没有ClsAdvertisement []来设置典型值。但我不知道如何初始化这个?

请帮帮我。非常感谢。


请注意,我已阅读此链接:&#34; What is a NullReferenceException, and how do I fix it?&#34;。我无法从这个链接得到答案。我想知道我的方式是否良好,如果是的话,如何初始化这个,这个链接中没有提到。

3 个答案:

答案 0 :(得分:0)

qualities[0]为空。

第一行应该解决这个问题:

qualities[0] = new ClsAdvertisement();
qualities[0].typical = double.Parse(dr1["id"].ToString());
qualities[0].type = "id";
qualities[0].min = qualities[0].typical;

同样适用于阵列的所有其他元素。

答案 1 :(得分:0)

问题很可能是在填充其属性之前没有实例化相关对象。尝试这样做:

   if (dr1.IsDBNull(dr1.GetOrdinal("id")) == false)
   { 
      qualities[0] = new ClsAdvertisement();
      qualities[0].typical = double.Parse(dr1["id"].ToString());
      qualities[0].type = "id";
      qualities[0].min = qualities[0].typical;
   }

答案 2 :(得分:0)

实例化对象也可以在实例化数组时完成。请注意,质量数组本身可以添加到服务列表中。第三个优化是DataReader的使用。

        List<ClsAdvertisement[]> services = new List<ClsAdvertisement[]>();
        using (IDataReader dr1 = cmd1.ExecuteReader())
        { 
            while (dr1.Read())
            {
                ClsAdvertisement[] qualities = { new ClsAdvertisement(), new ClsAdvertisement(), new ClsAdvertisement() };

                if (!dr1.IsDBNull(dr1.GetOrdinal("id")))
                {
                    qualities[0].typical = double.Parse(dr1["id"].ToString());
                    qualities[0].type = "id";
                    qualities[0].min = qualities[0].typical;
                }
                if (!dr1.IsDBNull(dr1.GetOrdinal("a")))
                {
                    qualities[1].typical = double.Parse(dr1["a"].ToString());
                    qualities[1].type = "a";
                    qualities[1].min = qualities[1].typical - 1.0;
                }
                if (!dr1.IsDBNull(dr1.GetOrdinal("b")))
                {
                    qualities[2].typical = double.Parse(dr1["b"].ToString());
                    qualities[2].type = "b";
                    qualities[2].min = qualities[2].typical - 1.0;
                }

                services.Add(qualities);
            }
        }