.net generic problem

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

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;
}
 
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.
 
Back
Top