?
Solved

Accessing an Array of Tables in C#,throws Object reference not set to an instance of an object exception

Posted on 2007-12-04
4
Medium Priority
?
396 Views
Last Modified: 2013-11-26
I'm working on a web based report(ASP.NET 1.1 & C#)  and  essentially have an array of tables
which i am filling dynamically with rows and Cells from the results of a DataTable

Below is the a small sample that illustrates this.

For Starters.I'm not really sure if i can actually create an array of Tables
but i needed something that offered that functionality,and couldn't think of
any other option fast enough

The problem i'm having is i get an "Object reference Not set to an Instance of an object"
exception whenever i get to
1) td10[i].Text  or 2) Tbl[i].Rows.Add(trow)

Am perplexed.Please Advice

Table[] Tbl= new Table[count];
TableRow[] trow = new TableRow[count];						TableCell[] td10=new TableCell[count];
for(int i =0; i< count; i++)
{
										td10[i].Text="Hey";
							td10[i].Width=150;
						  trow[i].Cells.Add(td10[i]);                         
 
Tbl[i].Rows.Add(trow[i]);
 
}
						}

Open in new window

0
Comment
Question by:PremGss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 total points
ID: 20409173
when you declare an array of objects, like
      TableCell[] td10 = new TableCell[count]
the array object is created, but the elements into it are null. So you cannot access them before constructing a new element. Something like:

Table[] Tbl= new Table[count];
TableRow[] trow = new TableRow[count];
TableCell[] td10 = new TableCell[count];
for(int i =0; i< count; i++)
{
      td10[i] = new TableCell();   // this is required to avoid the exception
      td10[i].Text="Hey";
      td10[i].Width=150;

      trow[o] = new TableRow();   // required
      trow[i].Cells.Add(td10[i]);

      Tbl[i] = new Table();  // required
      Tbl[i].Rows.Add(trow[i]);
 }
      
besides this, your code looks strange, if count = 20, you create 20 tables, 20 rows and 20 cells.
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20409210
you may have a dataset to hold all tables and then process tables one by one from dataset.. like this
//
DataSet ds=new DataSet("all tables");
ds.Tables.add("Table1);
ds.Tables.add("Table2);
ds.Tables.add("Table3);

int count=0;
int Rowcount=9;
for(;ds.tables.count > count; count++)
{
//here if you know how many rows to add in each table then you may use inner loop to do that
otherwise.
ds.tables[0].rows.add(xxxxxxxxxxxxxxxx);

}


thanks
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20592635
Forced accept.

Computer101
EE Admin
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question