" 'StackOverflowExcepton' was unhandled " C# Windows App

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joedeene
    Contributor
    • Jul 2008
    • 579

    " 'StackOverflowExcepton' was unhandled " C# Windows App

    Hello there, I am having a problem and it is frustrating me because I've been trying to figure it out, and I've even modified the code a few times but the same exception occurs: " 'StackOverflowE xcepton' was unhandled "

    Project Details:

    Ok, I am creating a text file that will store database information of some customers of mine and I'm using the StreamReader and StreamWriter Class. I can successfully get the information and customer data written to the file, but when I try to retrieve it and store the retrieved data into the listbox I get that exception. I am using a class library/.DLL also in the project, which shouldn't matter because I can call the AddCustomer() function properly. Here's some of the code...

    Code:
     public string[] retrieveCustomers()
            {
                System.IO.StreamReader CustomerStreamReader = new System.IO.StreamReader("MyPath"); //This is the line the error is on
    
                int i = 0;
                string newLine;
                while ((newLine = CustomerStreamReader.ReadLine()) != null)
                {
                    retrieveCustomers().SetValue(newLine, i);
                    i++;
                }
    
                CustomerStreamReader.Close();
    
                return retrieveCustomers();
            }
    And I call it from the form's button like this;

    Code:
     private void btnReload_Click(object sender, EventArgs e)
            {
                listBoxCustomers.Items.Clear();
                string[] CustomersCollection = papers.retrieveCustomers(); //papers is my .DLL reference.
                foreach (string customerInformation in CustomersCollection)
                {
                    listBoxCustomers.Items.Add(customerInformation);
                }
            }
    But there's no error there, Just like to know why it like freezes, and how to fix it. It says "Make sure there is no infinite loop", I don't think I have one though, and it freezes right at the part where I'm declaring the StreamReader in the .DLL

    joedeene
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by joedeene
    Hello there, I am having a problem and it is frustrating me because I've been trying to figure it out, and I've even modified the code a few times but the same exception occurs: " 'StackOverflowE xcepton' was unhandled "

    Project Details:

    Ok, I am creating a text file that will store database information of some customers of mine and I'm using the StreamReader and StreamWriter Class. I can successfully get the information and customer data written to the file, but when I try to retrieve it and store the retrieved data into the listbox I get that exception. I am using a class library/.DLL also in the project, which shouldn't matter because I can call the AddCustomer() function properly. Here's some of the code...

    Code:
     public string[] retrieveCustomers()
            {
                System.IO.StreamReader CustomerStreamReader = new System.IO.StreamReader("MyPath"); //This is the line the error is on
    
                int i = 0;
                string newLine;
                while ((newLine = CustomerStreamReader.ReadLine()) != null)
                {
                    retrieveCustomers().SetValue(newLine, i);
                    i++;
                }
    
                CustomerStreamReader.Close();
    
                return retrieveCustomers();
            }
    And I call it from the form's button like this;

    Code:
     private void btnReload_Click(object sender, EventArgs e)
            {
                listBoxCustomers.Items.Clear();
                string[] CustomersCollection = papers.retrieveCustomers(); //papers is my .DLL reference.
                foreach (string customerInformation in CustomersCollection)
                {
                    listBoxCustomers.Items.Add(customerInformation);
                }
            }
    But there's no error there, Just like to know why it like freezes, and how to fix it. It says "Make sure there is no infinite loop", I don't think I have one though, and it freezes right at the part where I'm declaring the StreamReader in the .DLL

    joedeene
    Have you put breakpoint on line 3 to see if it gets called numerous times? Maybe you really do have a loop.

    Try wrapping the guts of the function in a Try {} Catch{} statement. That way you can catch the error and read its message.

    Try adding a line for console.writeli ne("Entered this method"); just above the problem line. If your output window gets slammed with messages then you know you have a loop.


    Wait wait wait... Line 15 is calling this function. The return loops back to call itself. Yep, its a loop.

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      Originally posted by tlhintoq
      Wait wait wait... Line 15 is calling this function. The return loops back to call itself. Yep, its a loop.
      So I can't return the function() after it's value has been changed? It only pauses on the streamreader line, and i'll try that try catch statement and get back to you.

      joedeene

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by tlhintoq
        ...
        Wait wait wait... Line 15 is calling this function. The return loops back to call itself. Yep, its a loop.
        Line 15 is never reached. The infinite looping is happening on line 9.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by r035198x
          Line 15 is never reached. The infinite looping is happening on line 9.
          Line 9 is also a loop.
          this smells of a VB programmer trying to learn c#.

          Try something like this:
          [code=c#]
          public string[] retrieveCustome rs()
          {
          List<string> retval = new List<string>();
          System.IO.Strea mReader CustomerStreamR eader = new System.IO.Strea mReader("MyPath "); //This is the line the error is on

          string newLine;
          while ((newLine = CustomerStreamR eader.ReadLine( )) != null)
          {
          retval.Add(newl ine);
          }
          CustomerStreamR eader.Close();

          return retval.ToArray( );
          }
          [/code]

          or
          [code=c#]
          public string[] retrieveCustome rs()
          {
          List<string> retval = new List<string>();
          System.IO.Strea mReader CustomerStreamR eader = new System.IO.Strea mReader("MyPath "); //This is the line the error is on

          while (!CustomerStrea mReader.EndOfSt ream)
          {
          retval.Add(Cust omerStreamReade r.ReadLine());
          }
          CustomerStreamR eader.Close();

          return retval.ToArray( );
          }

          [/code]

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Plater
            ..
            this smells of a VB programmer trying to learn c#.

            ...
            <Blocks nose because joe is about to walk in />

            Comment

            • joedeene
              Contributor
              • Jul 2008
              • 579

              #7
              Originally posted by Plater
              ...this smells of a VB programmer trying to learn c#....
              Ya, I did Visual Basic.Net for a while and decided to switch, thanks for the help. I'll try it when i get home :)

              joedeene

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by Plater
                this smells of a VB programmer trying to learn c#.
                I suppose that came off sounding a bit "mean".
                What I ment was, in VB you assign the return value of a function, directly TO the name of the function. so if I had a function called foo() that returned a string, inside the function I would set the return value by saying "foo="some string" (or close to that).
                How you were ever able to do recursion in VB, I don't know. In regular languages, referencing the name of the function will CALL the function.

                Comment

                • joedeene
                  Contributor
                  • Jul 2008
                  • 579

                  #9
                  Originally posted by Plater
                  I suppose that came off sounding a bit "mean".
                  What I ment was, in VB you assign teh return value of a function, directly TO the name of the function. so if I had a function called foo() that returned a string, inside the function I would set the return value by saying "foo="some string" (or close to that).
                  How you were ever able to do recursion in VB, I don't know. In regular languages, referencing the name of the function will CALL the function.
                  Nah, I didn't take it mean, iA said a while back that VB had/teaches lazy habits and I guess that's one of them, but ya I haven't worked with functions much in c# since VB but ya it does make sense that it calls that 'referencing the name of the function will CALL the function'. But it vb you could say something like that, well thanks for the knowledge, I'm home and I'm gonna try the above code now...

                  Edit: Yay it works, thanks a whole bunch, now I can continue with my database...

                  joedeene
                  Last edited by joedeene; Oct 30 '08, 08:50 PM. Reason: Added update

                  Comment

                  Working...