locked
why System.NullReferenceException occurred? RRS feed

  • Question

  • Hi,

    I am using the following code to activate existing MDI child and refresh the grid at the same time. It was working fine until today but suddenly I am getting:\

    System.NullReferenceException occurred
      Message=Object reference not set to an instance of an object.
      Source=Cure
      StackTrace:
           at cure_hospital_management.frmMain.repositoryItemFindFile_KeyDown(Object sender, KeyEventArgs e) in C:\Users\DELL USER\Documents\Visual Studio 2010\Projects\Cure\Cure\main_form.cs:line 1718
      InnerException: 


    here is the code:

    if (e.KeyCode == Keys.Enter && (sender as TextEdit).Text.Trim() != "")
    {
        this.Cursor = Cursors.WaitCursor;
    
        is_form_exists = false;
    
        foreach (Form search_resul_form in this.MdiChildren)
        {
            frmSearchResult search_form = search_resul_form as frmSearchResult;
    
            if ((string)search_form.Tag == "BROWSE_PATIENTS")
            {
                // search_form.Activated(null, null);
                search_form.find_what = "FILE";
                search_form.find_text1 = (sender as TextEdit).Text.Trim();
                search_form.find_text2 = null;
                search_form.browse_result();
                search_form.Activate();
                is_form_exists = true;
                break;
            }
        }
    
        if (is_form_exists == false)
        {
            frmSearchResult SearchResultForm = new frmSearchResult(public_var);
            SearchResultForm.Tag = "BROWSE_PATIENTS";
            SearchResultForm.MdiParent = this;
            SearchResultForm.Dock = DockStyle.Fill;
            SearchResultForm.find_what = "FILE";
            SearchResultForm.find_text1 = (sender as TextEdit).Text.Trim();
            SearchResultForm.find_text2 = null;
            SearchResultForm.Show();
        }
    
        (sender as TextEdit).Text = "";
    
        this.Cursor = Cursors.Default;
    }


    Saturday, February 23, 2013 12:54 PM

Answers

  • Your code seems to be OK & have problem only in the line

    & can be resolved
    if (Convert.ToString(search_form.Tag) == "BROWSE_PATIENTS") {


    Try to give details about the error at which the error occurs.

    It all Happenz Sendil

    • Proposed as answer by Mike Feng Tuesday, February 26, 2013 3:50 PM
    • Unproposed as answer by Mike Feng Tuesday, February 26, 2013 3:50 PM
    • Proposed as answer by Mike Feng Tuesday, February 26, 2013 3:51 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:02 PM
    Saturday, February 23, 2013 3:16 PM
  • At least search_form can be null, (sender as TextEdit) can be null. Can't you stop your application with a breakpoint or when an exception is thrown (Debug/Exceptions..)? Otherwise some logging should help.

    Miha Markic [MVP C#] http://blog.rthand.com

    • Proposed as answer by Mike Feng Tuesday, February 26, 2013 3:50 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:02 PM
    Saturday, February 23, 2013 4:47 PM

All replies

  • Your code seems to be OK & have problem only in the line

    & can be resolved
    if (Convert.ToString(search_form.Tag) == "BROWSE_PATIENTS") {


    Try to give details about the error at which the error occurs.

    It all Happenz Sendil

    • Proposed as answer by Mike Feng Tuesday, February 26, 2013 3:50 PM
    • Unproposed as answer by Mike Feng Tuesday, February 26, 2013 3:50 PM
    • Proposed as answer by Mike Feng Tuesday, February 26, 2013 3:51 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:02 PM
    Saturday, February 23, 2013 3:16 PM
  • At least search_form can be null, (sender as TextEdit) can be null. Can't you stop your application with a breakpoint or when an exception is thrown (Debug/Exceptions..)? Otherwise some logging should help.

    Miha Markic [MVP C#] http://blog.rthand.com

    • Proposed as answer by Mike Feng Tuesday, February 26, 2013 3:50 PM
    • Marked as answer by Mike Feng Thursday, February 28, 2013 2:02 PM
    Saturday, February 23, 2013 4:47 PM