0

I wrote the code that pulls all the links on a web page. I check with Arraylist to prevent the same link from appearing. But I get stackoverflow error if there are too many links on the site. I keep links in Arraylist for later use. How do I avoid this stackoverflow error? Could the cause of the problem be arraylist?

StackOverFlow Error

static ArrayList<String> linkleriTut = new ArrayList();    

public void linkleriCek(String url, String taramaTuru) throws IOException{
   try {
      Document doc = Jsoup.connect(url).get();
      Elements linkler = doc.select("a[href]");
      for (Element link : linkler) {
         if (!linkleriTut.contains(link.attr("abs:href"))) {
            linkleriTut.add(link.attr("abs:href"));
         }
      }
   }
}
catch (Exception e) {
}
if (taramaTuru.equals("Detaylı Tarama")) {
   while (k < linkleriTut.size()) {
      k++;
      linkleriCek(linkleriTut.get(k), taramaTuru);
   }
}
Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
Ömer Çelik
  • 77
  • 2
  • 11
  • 2
    You can try to remove the recursion in your method and implement it iteratively. – David Choweller Jan 31 '17 at 15:11
  • what is `k` here? `Element link : linkler` doesn't make sense too – Naman Jan 31 '17 at 15:12
  • or add more ram to your proces – XtremeBaumer Jan 31 '17 at 15:13
  • Use a `Set` instead of a `List` if you want each link to only be stored once – yitzih Jan 31 '17 at 15:18
  • It aims to get all the links by pulling the links inside the links. Suppose there are 200 links in the main link. I am also trying to find connections within these 200 links. – Ömer Çelik Jan 31 '17 at 15:20
  • @XtremeBaumer adding more ram will not solve a StackOverflowError, you are trolling right? --Xss – Redlab Jan 31 '17 at 15:21
  • @Redlab if you increase the amount of ram, the process can use, it might resolve the error, because stackoverflowerror is due to all available ram being used. so increasing it will result in more possible iterations. if its an infinite loop, this wont help, but if the loop can end, this might resolve it – XtremeBaumer Jan 31 '17 at 15:24
  • @XtremeBaumer i must disappoint you. "-Xss Sets the thread stack size (in bytes). Append the letter k or K to indicate KB, m or M to indicate MB, g or G to indicate GB. The default value depends on the platform:" - see http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html for default values ( 1MB for 64bit ) – Redlab Jan 31 '17 at 15:30
  • @Redlab http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror as i understand the accepted answer, increasing the heap (ram) also increases the amount of possible calls – XtremeBaumer Jan 31 '17 at 15:43

1 Answers1

1

When taramaTuru is Detaylı Tarama

if (taramaTuru.equals("Detaylı Tarama")) will always be true.

And thus for each entry in linkleriTut you call again linkleriCek(linkleriTut.get(k), taramaTuru)

which will after the initial loop again see that (taramaTuru.equals("Detaylı Tarama")) == true and thus your method keeps calling itself again and again which result in a StackOverFlowError

Redlab
  • 3,110
  • 19
  • 17