-2

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

At 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.


Note that I have read this link :"What is a NullReferenceException, and how do I fix it?". I Couldn`t get my answer from this link. I want to know if my way is good or not, and if yes,how to initialize this, which is not mentioned in that link .

Community
  • 1
  • 1
M K
  • 31
  • 8
  • Instantiate objects in your array. – Jeroen Vannevel Oct 08 '14 at 07:18
  • Take a look: [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Oct 08 '14 at 07:19
  • I have read this before. It said I should initialize this and check if it is null or not. But it doesn`t work for me. It is not null. id has something in it, I should initialize this. But I don't know how. And another question is that, Is my way a good way for what I am triyng to do? Thanks so much – M K Oct 08 '14 at 07:22
  • Did you at least pin down *what* is null? – nvoigt Oct 08 '14 at 07:23
  • I just told you. You create an array of type `ClsAdvertisement` with length 3, but you never create objects in it. So when you try to access a property of the object in index 0, you get this exception. Instantiate your object. – Jeroen Vannevel Oct 08 '14 at 07:25

3 Answers3

0

qualities[0] is null.

The first line should fix that:

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

Likewise for all other elements of the array.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

The problem is most likely that you don't instantiate the object in question before populating its properties. Try to do it like so:

   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;
   }
Tobias
  • 2,811
  • 2
  • 20
  • 31
0

Instantiating the objects can also be done when instantiating the array. Note that the qualities array itself can be added to the services list. Third optimization is the usage of the 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);
            }
        }
Zeratul75
  • 36
  • 2