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
393 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 250 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:needo_jee
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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