.net generic problem

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

Mark Fitzpatrick

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top