System.NullReference exception

B

Bhavya Shah

Hello,
I am facing a very strange problem in my application.
I have a form on which I select a path. I open the FolderBrowserDialog for
path selection. Once the path is selected I press a button "Search" for
searching documents on that path. This search process runs in a separate
thread so that I can search for multiple paths simultaneously. I face
problem here. After starting the search for one path (that is starting one
thread), when I click on the select folder button to open the
folderbrowserdialog for selecting another path, the application throws and
exception (System.NullReferenceException, Object reference not set to
instance of an object).
I do not get this error if I do not search in thread.
Could anyone tell me the reason for this...I have tried everything but could
not solve the error.

I am pasting my thread starting code here.
FileSvcCrawler is a class which searches the path for documents.
FileSvcCrawler.FileSvcCrawler objFileSvcCrawler = new
FileSvcCrawler.FileSvcCrawler(txtComputer.Text,int.Parse(nudRescanInt.Value.
ToString()), ChangeStatus);


ThreadStart objThreadStart = new ThreadStart(objFileSvcCrawler.Crawl);

Thread objThread = new Thread(objThreadStart);

objThread.Name = strComputerName ;

objThread.IsBackground = true;

objThread.Priority = ThreadPriority.Lowest;

objThread.Start();



Regards,
Bhavya Shah
 
N

Nicholas Paldino [.NET/C# MVP]

Bhavya,

I can't tell from the code that you provided, but you aren't throwing up
the folder browse dialog in another thread, are you? If so, then that would
be the cause of your problems, as all UI operations should occur on the UI
thread.

Hope this helps.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I think that your problem is the select button handler, not in the creation
of the thread which AFAIK should work fine.
Did you try to trace the application, it should give you the line where it
throw the exception.

Cheers,
 
1

100

Hi Bhavya,
The code you provided is not enough for us to tell where the problem is. Try
to find out what reference is null. Is it the reference to the
FolderBrowserDialog object or to some other object you use?
If you post the code for select-path button and serarch button event
handlers we may be able to help you out.

B\rgds
100
 
B

Bhavya Shah

Hello,

Thanks for you prompt reply.

I am pasting my code here so that you can understand the problem.

This the code When Search button is pressed.

This is the code for selecting the path for searching.

Here the system throws and exception on the line:

objFolderBrowserDialog.ShowDialog() statement. The exception is thrown from
System.Windows.Forms.dll from the ShowWindow() function. The exception of
System.NullReferenceException. And the main thing is the exception is thrown
only if I start the search in thread. Again, the exception is not thrown on
first attempt. The system allows me to select a path and then start search.
But when I try to select another path for searching system thrown an
exception. Again, the exception is thrown in case of any dialog I try to
open on this page. I tried to open a MessageBox instead of
FolderBrowserDialog just to check but I got the same exception. I also tried
bringing up my custom dialog on this page. I got the same exception.

private void btnPath_Click(object sender, System.EventArgs e)

{

FolderBrowserDialog objFolderBrowserDialog = new FolderBrowserDialog();

objFolderBrowserDialog.Description = "Select Path for selected Computer";

objFolderBrowserDialog.SelectedPath = @"\\" + txtComputer.Text + @"\";

objFolderBrowserDialog.ShowNewFolderButton = false;

if(objFolderBrowserDialog.ShowDialog()== DialogResult.OK)

{

while(true)

{

if(objFolderBrowserDialog.SelectedPath.ToUpper().IndexOf(txtComputer.Text)

{

break;

}

else

{

MessageBox.Show("Select path for the computer you have selected","Wrong Path
Selected",MessageBoxButtons.OK,MessageBoxIcon.Information);

objFolderBrowserDialog.SelectedPath = @"\\" + txtComputer.Text + @"\";

objFolderBrowserDialog.ShowDialog();

}

}

txtPath.Text = objFolderBrowserDialog.SelectedPath;

btnSelectMachine.Enabled = false;

btnAdd.Enabled = true;

btnAddNCrawl.Enabled = true;

}

}





private void btnAddNCrawl_Click(object sender, System.EventArgs e)

{

AddScanPathDetails();

// CallBack ChangeStatus = new
CallBack(ILink.Knowledge_Cube.frmStatus.OnChangeStatus);

// strComputerName = txtComputer.Text;

FileSvcCrawler.FileSvcCrawler objFileSvcCrawler = new
FileSvcCrawler.FileSvcCrawler(txtComputer.Text,int.Parse(nudRescanInt.Value.
ToString()), ChangeStatus);


ThreadStart objThreadStart = new ThreadStart(objFileSvcCrawler.Crawl);

Thread objThread = new Thread(objThreadStart);

objThread.Name = strComputerName ;

objThread.IsBackground = true;

objThread.Priority = ThreadPriority.Lowest;

objThread.Start();

}

The FileSvcCrawler class is in a separate dll.

I would be very thankful to you if you could give me some hint regarding the
error.

Regards,

Bhavya Shah
 

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

Similar Threads


Top