Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix a stack overflow Error in java?

I have a class Movie with a static array Movie[] movies. I implement the Comparable and i overload the method compareTo. If the likes of a movie are same with the likes of another movie then i compare them with alphabetical order. I have to create a quicksort implementation to sort an array of movies. But in line return this.compareTo(m); i got a stack overflow Error. How i am supposed to fix this ?

 public int compareTo(Movie m) {
            if (this.likes == m.likes) {
                //DefaultComparator cmp = new DefaultComparator();
                return this.compareTo(m);
            } else if (this.likes > m.likes) {
                return 1;
            } else {
                return -1;
            }

        }

        public static Movie[] sort(Movie[] m) {
            if (m == null || m.length == 0) {
                return null;
            } else {
                movies = m;
                quicksort(0, movies.length - 1); // sort the entire array
                return movies;
            }
        }

        public static void quicksort(int left, int right) {
            int i = left;
            int j = right;
            Movie pivot = movies[left + (right - left) / 2];
            while (i <= j) {
                while (movies[i].compareTo(pivot) == -1) {
                    i++;
                }
                while (movies[j].compareTo(pivot) == 1) {
                    j--;
                }
                if (i <= j) {
                    exch(i, j);
                    i++;
                    j--;
                }

            }
            if (left < j) {
                quicksort(left, j);
            }
            if (i < right) {
                quicksort(i, right);
            }
        }

        public static void exch(int i, int j) {
            Movie temp = movies[i];
            movies[i] = movies[j];
            movies[j] = temp;
        }
like image 661
Damis Berzovitis Avatar asked Dec 09 '15 12:12

Damis Berzovitis


2 Answers

public int compareTo(Movie m) {
            if (this.likes == m.likes) {
                //DefaultComparator cmp = new DefaultComparator();
                return this.compareTo(m);
            } else if (this.likes > m.likes) {
                return 1;
            } else {
                return -1;
            }

        }

If this.likes == m.likes returns true, you'll start an infinite recursive loop, after all, both the 'this' and 'm' are the same in the next iteration(s), so will their values of m be. That's your problem.

like image 181
Stultuske Avatar answered Oct 22 '22 08:10

Stultuske


In

if (this.likes == m.likes) {
    return this.compareTo(m);
}

compareTo calls itself recursively, which would cause infinite recursion, which can only end in StackOverflowError.

You should decide how you wish to compare the two Movie objects in case the number of likes is equal. Either compare them by another property of Movie or return 0.

like image 31
Eran Avatar answered Oct 22 '22 10:10

Eran