Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. loop error

loop error

Scheduled Pinned Locked Moved C#
questionhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASGill
    wrote on last edited by
    #1

    Hi. bool FrmIsOpen = false; for (int i=0; i < **this.MdiParent.MdiChildren.Length**; i++) { if (this.MdiParent.MdiChildren[i].GetType() == typeof(FrmPassword)) { FrmIsOpen = true; break; } } if (!FrmIsOpen) { FrmPassword r = new FrmPassword(); r.MdiParent = this; r.Show(); } An unhandled exception of type 'System.NullReferenceException' occurred in CLINICINFORMATIONSYSTEM.exe Additional information: Object reference not set to an instance of an object. the error's pointing to the line that has been bold...i dont understand what is it trying to say?.... CODER

    J D 2 Replies Last reply
    0
    • A ASGill

      Hi. bool FrmIsOpen = false; for (int i=0; i < **this.MdiParent.MdiChildren.Length**; i++) { if (this.MdiParent.MdiChildren[i].GetType() == typeof(FrmPassword)) { FrmIsOpen = true; break; } } if (!FrmIsOpen) { FrmPassword r = new FrmPassword(); r.MdiParent = this; r.Show(); } An unhandled exception of type 'System.NullReferenceException' occurred in CLINICINFORMATIONSYSTEM.exe Additional information: Object reference not set to an instance of an object. the error's pointing to the line that has been bold...i dont understand what is it trying to say?.... CODER

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      It's trying to tell you that something is null; this.MdiParent or MdiParent.MdiChildren is null. Check for that condition before entering the loop. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

      A 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        It's trying to tell you that something is null; this.MdiParent or MdiParent.MdiChildren is null. Check for that condition before entering the loop. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

        A Offline
        A Offline
        ASGill
        wrote on last edited by
        #3

        Hi, Im sorry Judah, im a beginner...i would appreciate it if you could show me how i can do that. thx in advance CODER

        J 1 Reply Last reply
        0
        • A ASGill

          Hi, Im sorry Judah, im a beginner...i would appreciate it if you could show me how i can do that. thx in advance CODER

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          bool FrmIsOpen = false;

          if(this.MdiParent != null && this.MdiParent.MdiChildren != null)
          {
          for (int i=0; i < this.MdiParent.MdiChildren.Length; i++)
          {
          if (this.MdiParent.MdiChildren[i].GetType() == typeof(FrmPassword))
          {
          FrmIsOpen = true;
          break;
          }
          }
          }

          if (!FrmIsOpen)
          {
          FrmPassword r = new FrmPassword();
          r.MdiParent = this;
          r.Show();
          }

          --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

          1 Reply Last reply
          0
          • A ASGill

            Hi. bool FrmIsOpen = false; for (int i=0; i < **this.MdiParent.MdiChildren.Length**; i++) { if (this.MdiParent.MdiChildren[i].GetType() == typeof(FrmPassword)) { FrmIsOpen = true; break; } } if (!FrmIsOpen) { FrmPassword r = new FrmPassword(); r.MdiParent = this; r.Show(); } An unhandled exception of type 'System.NullReferenceException' occurred in CLINICINFORMATIONSYSTEM.exe Additional information: Object reference not set to an instance of an object. the error's pointing to the line that has been bold...i dont understand what is it trying to say?.... CODER

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            My first question would be where is this code sitting? Is it in a child form or an MDIParent? It looks like a parent form has this code, in which case the parent form has no MDIParent. This reference would be null, and therefor, your reference to 'this.MdiParent.MdiChildren.Length' would return null. If this code is in a parent form, then your reference should be this.MdiChildren.Length. RageInTheMachine9532

            A 1 Reply Last reply
            0
            • D Dave Kreskowiak

              My first question would be where is this code sitting? Is it in a child form or an MDIParent? It looks like a parent form has this code, in which case the parent form has no MDIParent. This reference would be null, and therefor, your reference to 'this.MdiParent.MdiChildren.Length' would return null. If this code is in a parent form, then your reference should be this.MdiChildren.Length. RageInTheMachine9532

              A Offline
              A Offline
              ASGill
              wrote on last edited by
              #6

              its in a parent form. i tried editing the code your way..but it is still able to open up two same forms... bool FrmIsOpen = false; if(this.MdiParent != null && this.MdiChildren != null) { for (int i=0; i < this.MdiChildren.Length; i++) { if (this.MdiParent.MdiChildren[i].GetType() == typeof(FrmPassword)) { FrmIsOpen = true; break; } } } if (!FrmIsOpen) { FrmPassword r = new FrmPassword(); r.MdiParent = this; r.Show(); } CODER

              D 1 Reply Last reply
              0
              • A ASGill

                its in a parent form. i tried editing the code your way..but it is still able to open up two same forms... bool FrmIsOpen = false; if(this.MdiParent != null && this.MdiChildren != null) { for (int i=0; i < this.MdiChildren.Length; i++) { if (this.MdiParent.MdiChildren[i].GetType() == typeof(FrmPassword)) { FrmIsOpen = true; break; } } } if (!FrmIsOpen) { FrmPassword r = new FrmPassword(); r.MdiParent = this; r.Show(); } CODER

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                OK. The loop now runs but your references in your if statement are still screwed up. Use this instead:

                bool FrmIsOpen = false;

                if (this.MdiChildren.Length > 0)
                for (int i=0; i < this.MdiChildren.Length; i++)
                {
                if (this.MdiChildren[i].GetType() == GetType(FrmPassword))
                {
                FrmIsOpen = true;
                break;
                }
                }
                }
                 
                if (!FrmIsOpen)
                {
                FrmPassword r = new FrmPassword();
                r.MdiParent = this;
                r.Show();
                }

                The first bolded statement was changed because 'this.MDIParent' will always return null if 'this' is an MDIParent. This was changed to checking if the MDIChildren collection has anything in it. In the second bolded statement, the 'typeof()' reference was changed to 'GetType()'. If you have to, you could change this to 'Type.GetType()'. This should take care of your problem. RageInTheMachine9532

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups