0

I have a winforms file search application that runs with no errors in visual studio, but when i publish it i get a "object not set to an instance of object error"

The backgroundworker code which is the point where it fails is :

    private void backGroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        e.Result = RunSearch();
        PopulateUi((FilteredList)e.Result);
    }

    private void backGroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        try
        {
                var ans = (FilteredList)e.Result;
                toolStripStatusLabel1.Text += "Folders found: " + ans.FolderCount + " | Files found: " + ans.FileCount;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

The messagebox shows the exception text.What do i need to do to correct this?

EDIT This question is different to the one referred to above as its about a concurrency issue and not simply an object being null. As stated above, the application runs with no errors before publishing.

thanks

Edit 2

I have tried creating a private object ( _x) in the class and assigning the result to it, but it still shows as null in the RunWorkerCompleted function. The edited code looks like this:

    private void backGroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        e.Result = RunSearch();
        _x = e.Result;
        PopulateUi((FilteredList)e.Result);
    }

    private void backGroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        try
        {
            if (_x == null)
            {
                MessageBox.Show("e is null");
            }

            var ans = (FilteredList)_x;
            toolStripStatusLabel1.Text += "Folders found: " + ans.FolderCount + " | Files found: " + ans.FileCount;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Nick
  • 2,475
  • 5
  • 23
  • 49
  • We can't do much from here, the only way to find your exception is debugging. You can run your application after publishing and open Visual Studio > Debugger > Attach to process. Remove your catch and the debugger will be able to stop right where the exception happens. – Thadeu Fernandes May 31 '18 at 10:01
  • Thanks @ThadeuFernandes. I tried that and the code stops at the last line where im trying to show results on the toolstriplabel. the reason is that e.Result is null in Runworkercompleted. – Nick May 31 '18 at 10:18
  • You're welcome! Glad to know it worked for you. – Thadeu Fernandes May 31 '18 at 10:21
  • The RunWorkerCompleted event handler is not good enough, it does not deal with the possibility that the worker failed with an exception. No result, kaboom. Checking e.Error is not optional. – Hans Passant May 31 '18 at 10:39
  • @HansPassant thanks for the reply. I also tried with if(e.Error != null) ... but had the same result. – Nick May 31 '18 at 11:55
  • Well, the second snippet is not correct either, it just keeps motoring even if the x variable is null. Do make sure to test this on your own machine, temporarily add `throw new Exception("testing");` at the start of DoWork. – Hans Passant May 31 '18 at 12:08

0 Answers0