Object reference not set to an instance of an object.

G

Guest

Hi Everyone,

It has been a long time. Anyhow, I am writing this app that is suppose to
access a directory on the server and list all the directories files and bind
it to a Repeater control. This is the source or codebehind rather:

private void Page_Load(Object source, EventArgs e)
{
If(!Page.IsPostBack)
{
...
showDirectoryContents(Server.MapPath(Directory Path));
...
}
}
private void showDirectoryContents(string Path)
{
DirectoryInfo dir=new DirectoryInfo(Path);
FileInfo[] fileCollection=dir.GetFiles();
Repeater1.DataSource=fileCollection;
Repeater1.DataBind();
}
The error I am getting is as follows:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 210: DirectoryInfo dir=new DirectoryInfo(Path);
Line 211: FileInfo[] fileCollection=dir.GetFiles();
Line 212: Repeater1.DataSource=fileCollection; <---this line is the
source of the problem.
Line 213: Repeater1.DataBind();
Line 214: }

I've scripted the repeater control into the form as it should be and set the
id to Repeater1 so I don't know what I'm doing wrong. Can someone tell me
what I'm missing here?

Thank you.

Sam-
 
L

Laurent Bugnion

Hi,
Hi Everyone,

It has been a long time. Anyhow, I am writing this app that is suppose to
access a directory on the server and list all the directories files and bind
it to a Repeater control. This is the source or codebehind rather:

Do you have a aspx.designer.cs file under the ASPX file in the Solution
explorer? Sometimes (especially when porting a 1.1 application to 2.0),
the file is not generated automatically, and then the controls are not
created.

Two ways around that: Declare the "Repeater1" yourself in the code
behind (must be protected), or recreate the ASPX file, copy-paste the
controls inside it, compile and make sure that the designer.cs file is
generated correctly this time.

HTH,
Laurent
private void Page_Load(Object source, EventArgs e)
{
If(!Page.IsPostBack)
{
...
showDirectoryContents(Server.MapPath(Directory Path));
...
}
}
private void showDirectoryContents(string Path)
{
DirectoryInfo dir=new DirectoryInfo(Path);
FileInfo[] fileCollection=dir.GetFiles();
Repeater1.DataSource=fileCollection;
Repeater1.DataBind();
}
The error I am getting is as follows:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 210: DirectoryInfo dir=new DirectoryInfo(Path);
Line 211: FileInfo[] fileCollection=dir.GetFiles();
Line 212: Repeater1.DataSource=fileCollection; <---this line is the
source of the problem.
Line 213: Repeater1.DataBind();
Line 214: }

I've scripted the repeater control into the form as it should be and set the
id to Repeater1 so I don't know what I'm doing wrong. Can someone tell me
what I'm missing here?

Thank you.

Sam-
 

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

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top