.net generic problem

Discussion in 'ASP .Net' started by Jeff, Nov 2, 2006.

  1. Jeff

    Jeff Guest

    Hey

    asp.net 2.0

    The code below throws an "Object reference not set to an instance of an
    object" exception at the "logitems.Add(item);" line...

    the "LogItem item" object is successfully created, but it the exception is
    thrown when trying to put this object on the list... maybe it's because the
    "LogItem item" isn't declared as a <List>.. I'm not sure.. well, I'm more
    like a newbie when it comes to Generic .net.....

    Any suggestions on how to solve this is welcome!

    public static List<LogItem> GetLog()
    {
    List<LogItem> logitems = null;
    SqlConnection conn = new
    SqlConnection(ConfigurationManager.ConnectionStrings["aspnet_DB"].ConnectionString);
    SqlCommand cmd = new SqlCommand("AH_Get", conn);
    SqlDataReader reader;

    cmd.CommandType = CommandType.StoredProcedure;
    try
    {
    conn.Open();
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    LogItem item = new LogItem(reader.GetString(0),
    reader.GetString(1), reader.GetDateTime(2));
    logitems.Add(item);
    }
    }
    catch (SqlException ex)
    {
    }
    finally
    {
    conn.Close();
    }
    return logitems;
    }
     
    Jeff, Nov 2, 2006
    #1
    1. Advertising

  2. I haven't played with generics yet myself, but the logitems variable has to
    be instantiated as some sort of list in order to use it. Although you've
    declared it, it's still not instantiated so it's throwing an error because,
    as you've coded, it's still set to null.

    --
    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006

    "Jeff" <> wrote in message
    news:%23pszlVh$...
    > Hey
    >
    > asp.net 2.0
    >
    > The code below throws an "Object reference not set to an instance of an
    > object" exception at the "logitems.Add(item);" line...
    >
    > the "LogItem item" object is successfully created, but it the exception is
    > thrown when trying to put this object on the list... maybe it's because
    > the "LogItem item" isn't declared as a <List>.. I'm not sure.. well, I'm
    > more like a newbie when it comes to Generic .net.....
    >
    > Any suggestions on how to solve this is welcome!
    >
    > public static List<LogItem> GetLog()
    > {
    > List<LogItem> logitems = null;
    > SqlConnection conn = new
    > SqlConnection(ConfigurationManager.ConnectionStrings["aspnet_DB"].ConnectionString);
    > SqlCommand cmd = new SqlCommand("AH_Get", conn);
    > SqlDataReader reader;
    >
    > cmd.CommandType = CommandType.StoredProcedure;
    > try
    > {
    > conn.Open();
    > reader = cmd.ExecuteReader();
    > while (reader.Read())
    > {
    > LogItem item = new LogItem(reader.GetString(0),
    > reader.GetString(1), reader.GetDateTime(2));
    > logitems.Add(item);
    > }
    > }
    > catch (SqlException ex)
    > {
    > }
    > finally
    > {
    > conn.Close();
    > }
    > return logitems;
    > }
    >
     
    Mark Fitzpatrick, Nov 2, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. T. Davis
    Replies:
    4
    Views:
    20,148
    SeanMcIlvenna
    May 19, 2009
  2. Swati
    Replies:
    1
    Views:
    613
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 13, 2004
  3. Murat Tasan
    Replies:
    1
    Views:
    8,076
    Chaitanya
    Feb 3, 2009
  4. Replies:
    2
    Views:
    448
  5. minlearn
    Replies:
    2
    Views:
    464
    red floyd
    Mar 13, 2009
Loading...

Share This Page