NullReferenceException同时调整类的数组列表 [英] NullReferenceException while tring to careate a list of array of class

查看:67
本文介绍了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 < span class =code-keyword> string type { get ; set ; }
public double 典型{ get ; set ; }
public double min { get ; set ; }
}

受保护 void MyFuction()
{
列表< ClsAdvertisement [] > services = new 列表< ClsAdvertisement [] < span class =code-keyword>>
();
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
while (dr1 .Read())
{
ClsAdvertisement [] qualities = new ClsAdvertisement [ 3 ];

if (dr1.IsDBNull(dr1.GetOrdinal( id))== false
{
质量[ 0 ]。典型= double .Parse(dr1 [ ID]的ToString());
质量[ 0 ]。type = ID;
质量[ 0 ]。min =质量[ 0 ]。典型;
}
if (dr1.IsDBNull(dr1.GetOrdinal( a))== false
{
质量[ 1 ]。典型= double .Parse(dr1 [ < span class =code-string> a
]。ToString());
质量[ 1 ]。type = 一个;
质量[ 1 ]。min =质量[ 1 ]。典型 - 1 0 ;
}
if (dr1.IsDBNull(dr1.GetOrdinal( b))== false
{
质量[ 2 ]。典型= double .Parse(dr1 [ < span class =code-string> b
]。ToString());
质量[ 2 ]。type = b;
质量[ 2 ]。min =质量[ 2 ]。典型 - 1 0 ;
}

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





这是一个很好的方法吗?



如果,是的,我得到错误System.NullReferenceException:Object reference not set to a object of objectin the line:qualities [0] .typical = double.Parse(dr1 [id] .ToString());



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



请帮帮我。非常感谢。

解决方案

我相信,您收到错误是因为您引用的列ID列为空。如您的代码所述,



 dr1 [id] 





您正在使用赋值运算符,因此错误将通过右侧生成,您可以在其中看到,数据是从dr1(SQL数据持有者)和项目中提取的。重新获取是ID。



当传递的值是 null 时,会生成NullReferenceException。因此,您需要确保将值传递给需要值的函数或运算符。



如你所说,你认为

 ClsAdvertisement [] 

不存在,如果是这样, Visual Studio将通过Line(当前显示错误),然后在该特定行上抛出错误,并且会说找不到类型[your_type_here]的ClsAdvertisement []。您是否缺少引用?像这样的东西。在你的情况下,这不是错误。



从我的博客文章中了解有关代码执行中的空错误的更多信息:代码执行中的什么是空错误[ ^ ]


你是得到错误,因为对象quanties有三个项目,并且都是null。



你可以使用List代替数组这样。

  public   class  ClsAdvertisement 
{
public string type { get ; set ; }
public double 典型{ get ; set ; }
public double min { get ; set ; }
}
public class ClsService
{
public 列表< clsadvertisement>>}

受保护 void MyFuction()
{
List< clsservice> services = new List< clsservice>();
List< clsadvertisement> service = new List< clsadvertisement>();

service.Add( new ClsAdvertisement()
{
typical = 1
type = id
min = 1
});
service.Add( new ClsAdvertisement()
{
typical = 2
type = 1
min = 2
});
service.Add( new ClsAdvertisement()
{
typical = 3
type = b
min = 3
});
services.Add( new ClsService()
{
service = service
});
}





通过这个你可以添加多少项目。


谢谢你的答案。

关于第一个解决方案,我的数据不是空的。检查它是否为空是我们可以做的事情,但是这里我的数据不是空的。

而且,关于第二个解决方案,我想填写id.typical和id.min中的数据,来自我在SQL中的表格。

但是,非常感谢你们两位。

我可以通过以下代码得到答案:

< pre lang =c#>列表< clsadvertisement [] > services = new 列表< clsadvertisement [] > ();
使用(IDataReader dr1 = cmd1.ExecuteReader())
{
while (dr1.Read())
{
ClsAdvertisement [] qualities = { new ClsAdvertisement(), new ClsAdvertisement(), new ClsAdvertisement()};

if (!dr1.IsDBNull(dr1.GetOrdinal( id)))
{
质量[ 0 ]。典型= double .Parse(dr1 [ id]。的ToString());
质量[ 0 ]。type = ID;
质量[ 0 ]。min =质量[ 0 ]。典型;
}
if (!dr1.IsDBNull(dr1.GetOrdinal( < span class =code-string> a
)))
{
质量[ 1 ]。典型= double .Parse(dr1 [ a]的ToString());
质量[ 1 ]。type = 一个;
质量[ 1 ]。min =质量[ 1 ]。典型 - 1 0 ;
}
if (!dr1.IsDBNull(dr1.GetOrdinal( < span class =code-string> b
)))
{
质量[ 2 ]。典型= double .Parse(dr1 [ b]的ToString());
质量[ 2 ]。type = b;
质量[ 2 ]。min =质量[ 2 ]。典型 - 1 0 ;
}

services.Add(品质);
}
}





或者,我可以做到以下几点:



  if (dr1.IsDBNull(dr1.GetOrdinal(  id))==  false 
{
质量[ 0 ] = new ClsAdvertisement();
质量[ 0 ]。典型= double .Parse(dr1 [ id]。ToString());
质量[ 0 ]。type = ID;
质量[ 0 ]。min =质量[ 0 ]。典型;
}







问题很可能是我没有在填充其属性之前实例化相关对象。



感谢大家。

祝你好运


Hello every body.

I have a table in SQL that includes different services. Each service have some qualities (It means each row, have different columns in SQL). For example:

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

s2: id2, a2, b2

...

I want to have a list of these services in c#.

For some reasons, each column, should have some attributes. For instance:

For a1 form s1, we should have these:

a1.type, a1.typical, a1.min

This is what I did:

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();



Is it a good way for doing this?

If,yes, I get the error"System.NullReferenceException: Object reference not set to an instance of an object" in line: " qualities[0].typical = double.Parse(dr1["id"].ToString());"

From my searches, I knew the reason is that there is no ClsAdvertisement[] to set the typical for. But I don`t know how to initialize this?

Please help me. Thanks a lot.

解决方案

I believe, you're getting the error because the Column you're referencing to, the ID column, is null. As stated in your code,

dr1["id"]



You're using the assignment operator, so the error would be generated through right hand side, where you can see, the data is being extracted from dr1 (which is SQL data holder) and the item you're fetching is ID.

NullReferenceException is generated when the value being passed is null. So, you need to make sure that the value is being passed to a function or operator which requires value.

As you're saying, you think

ClsAdvertisement[]

is not present, if that was the case, Visual Studio would have passed the Line (that is currently displaying the error) and then threw an error on that particular line and would have said, "ClsAdvertisement[] not found for the type [your_type_here]. Are you missing a reference?" Something like this. In your case, this is not th error.

Learn more about null errors in code execution from my blog post: What is a null error in code Execution[^]


you are getting error since the object quanties has three items and all are null.

you can use List instead of array Like this.

public class ClsAdvertisement
{
  public string type { get; set; }
  public double typical { get; set; }
  public double min { get; set; }
}
public class ClsService
{
  public  List<clsadvertisement>>}

protected void MyFuction()
{
  List<clsservice> services = new List<clsservice>();
  List<clsadvertisement> service=new List<clsadvertisement>();
  
  service.Add(new ClsAdvertisement()
   {
     typical=1,
     type = "id",
     min = 1
   });
  service.Add(new ClsAdvertisement()
   {
     typical = 2,
     type = "1",
     min = 2
   });
  service.Add(new ClsAdvertisement()
   {
     typical = 3,
     type = "b",
     min = 3
   });
   services.Add(new ClsService()
       {
          service = service
       });
}



By this you can add as many as items.


Thanks for your answers.
About the first solution, My data is not null. Checking if it is not null is some thing we can do, but here my data is not null.
And, about the second solution, I want to fill data in id.typical and id.min, from my table in SQL.
However, Thanks a lot both of you.
I could get my answer with the following codes:

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);
           }
       }



Or, I could do just the following:

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;
  }




The problem is most likely that I didn't instantiate the object in question before populating its properties.

Thanks for all of you.
Good Luck


这篇关于NullReferenceException同时调整类的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆