Results 1 to 5 of 5
  1. #1
    csharp100 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Exception in thread "main" java.lang.StackOverflowError

    Hello everyone! As the title says I am having a problem with my code. There error I am getting is:
    Java Code:
    Exception in thread "main" java.lang.StackOverflowError
    	at java.util.LinkedList.get(LinkedList.java:315)
    	at probtest.prob.iterate(prob.java:66)
    	at probtest.prob.iterate(prob.java:93)
    	at probtest.prob.iterate(prob.java:88)
    Here is my code:
    Java Code:
    import java.util.*;
    
    public class prob
    {
      public int probType;
      public String src;
      public LinkedList<String> dest;
      public Answer done;
    
      public prob(int num, String node1)
      {
        probType = num;
        src = node1;
        dest = new LinkedList<String>();
        done = new Answer(src);
      }
    
      public prob(int num, String node1, LinkedList<String> node2)
      {
        probType = num;
        src = node1;
        dest = node2;
        done = new Answer(src);
      }
    
      public void solve(data_list d)
      {
        if (probType == 1)
        {
          String tempStr = dest.get(0);
          iterate(done, d, src, tempStr);
        }
        if (probType == 2)
        {
          LinkedList<String> temp1 = dest;
          iterate(done, d, src, temp1);
        }
        if (probType == 3)
        {
          LinkedList<String> temp2 = new LinkedList<String>();
          for (int i = 0; i < d.num_nodes; i++)
          {
            temp2.add(d.cities[i]);
          }
          iterate(done, d, src, temp2);
        }
        done.printAnswer();
      }
    
      public void iterate(Answer a, data_list d, String s1, String s2)
      {
        int len = d.list.size();
        data temp;
        for (int i = 0; i < len; i++)
        {
          temp = d.list.get(i);
          if (s1.equals(temp.src) && s2.equals(temp.dest))
          {
            a.add(temp.cost, temp.dest);
            if(done.total == 0 || a.total < done.total)
            {
              done = a;
            }
            return;
          }
          if (s2.equals(temp.src) && s1.equals(temp.dest))
          {
            a.add(temp.cost, temp.src);
            if(done.total == 0 || a.total < done.total)
            {
              done = a;
            }
            return;
          }
          if (s1.equals(temp.src) && !s2.equals(temp.dest))
          {
            a.add(temp.cost, temp.dest);
            iterate(a, d, temp.dest, s2);
          }
          if (s1.equals(temp.dest) && !s2.equals(temp.src))
          {
            a.add(temp.cost, temp.src);
            iterate(a, d, temp.src, s2);
          }
        }
      }
    
      public void iterate(Answer a, data_list d, String s, LinkedList<String> lis)
      {
        LinkedList<String> l = new LinkedList<String>();
        for ( int i = 0; i < lis.size(); i++ )
        {
          l.add(lis.get(i));
        }
        int len = d.list.size();
        int len2 = l.size();
        data temp;
        for (int i = 0; i < len; i++)
        {
          temp = d.list.get(i);
          for (int j = 0; j < len2; j++)
          {
            if (s.equals(temp.src) && l.get(j).equals(temp.dest))
            {
              a.add(temp.cost, temp.dest);
              String prev = l.remove(j);
              if((done.total == 0 || a.total < done.total) && l.size() == 0)
              {
                done = a;
                return;
              }
              iterate(a, d, prev, l);
              len2 = l.size();
              j--;
              continue;
            }
            if (temp.dest.equals(s) && temp.src.equals(l.get(j)))
            {
              a.add(temp.cost, temp.src);
              String prev = l.remove(j);
              if((done.total == 0 || a.total < done.total) && l.size() == 0)
              {
                done = a;
                return;
              }
              iterate(a, d, prev, l);
              len2 = l.size();
              j--;
              continue;
            }
            if (temp.src.equals(s) && !temp.dest.equals(l.get(j)))
            {
              a.add(temp.cost, temp.dest);
              iterate(a, d, temp.dest, l);
              continue;
            }
            if (temp.dest.equals(s) && !temp.src.equals(l.get(j)))
            {
              a.add(temp.cost, temp.src);
              iterate(a, d, temp.src, l);
              continue;
            }
          }
        }
      }
    }
    This is just one class and can provide others if needed, but do not think they will be needed. My question is what could be causing this and how do I fix it? Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.StackOverflowError

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    csharp100 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.StackOverflowError

    So what if it is cross posted? Is that against the rules? If more people spent time actually trying to help as opposed to criticizing a post, I would think more people such as myself, would be more inclined to help others.
    Last edited by csharp100; 04-05-2012 at 04:34 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Exception in thread "main" java.lang.StackOverflowError

    Quote Originally Posted by csharp100 View Post
    So what if it is cross posted? Is that against the rules? If more people spent time actually trying to help as opposed to criticizing a post, I would think more people such as myself, would be more inclined to help others.
    Way to go, csharp100. Now you're going to have people falling over each other trying to help you. Not.

    More to the point, where do you see criticism in Norm's post? I see a simple statement of fact. So all that your knee-jerk reaction proves is that you believe cross posting to be wrong, but went ahead and did it anyways.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Exception in thread "main" java.lang.StackOverflowError

    Quote Originally Posted by csharp100 View Post
    So what if it is cross posted? Is that against the rules? If more people spent time actually trying to help as opposed to criticizing a post, I would think more people such as myself, would be more inclined to help others.
    Again, it's not criticism, it is warning to other volunteers here to check the cross-post so as not to waste time answering a question that's already been answered elsewhere. I don't know about you, but I value my free time, and I very much appreciate it if posters let us know up front that they've cross-posted and where they've done so because it shows that they have consideration for my time as well.

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. Replies: 1
    Last Post: 12-02-2011, 06:55 PM
  3. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  4. Replies: 4
    Last Post: 02-01-2011, 08:52 AM
  5. Replies: 5
    Last Post: 06-22-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •