Results 1 to 3 of 3
  1. #1
    cele1989 is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default GSON causing stack overflow error?

    When I execute the code beneath in Netbeans I get perfect result. Getting the 2 lines in comment, out of comment resulting in a Stack overflow error:
    Exception in thread "main" java.lang.StackOverflowError
    at java.util.ArrayList.get(ArrayList.java:322)
    at com.google.gson.stream.JsonWriter.peek(JsonWriter. java:354)
    at com.google.gson.stream.JsonWriter.beforeValue(Json Writer.java:585)
    at com.google.gson.stream.JsonWriter.open(JsonWriter. java:318)
    ...

    Why is this happening and how can I fix it?



    import com.google.gson.Gson;
    import java.util.List;
    import persist.Reeks;

    public class TestHibernate {

    public static void main(String[] args) {
    List<Reeks> reeksen;
    try {
    reeksen = Functionalities.alleReeksenOpvragen();
    for (int i = 0; i < reeksen.size(); i++) {
    System.out.println("reeks: " + reeksen.get(i).getId() + " - " + reeksen.get(i).getNaam() + " - " + reeksen.get(i).getDatumuitgave());
    }
    //String json = new Gson().toJson(reeksen);
    //System.out.println(json);

    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    }

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: GSON causing stack overflow error?

    Well, you're out of Stack space. Try running with -Xms256M -Xmx512M flags (or larger if you have to) to the VM. (min-stack 256MB, max-stack 512MB), etc.

    Edit: I don't use netbeans, so can't help as to where the VM flags are set.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: GSON causing stack overflow error?

    Nope.
    That increases the heap, and has nothing to do with the stack.

    The OP's code is going into a recursive loop.
    Since it's JSON I suspect there are recursive dependencies in the objects they are trying to convert into a JSON string.
    That is class A has an attribute referring to class B, which also has an attribute referring back to class A.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Stack Overflow?
    By kyle_maddisson in forum New To Java
    Replies: 5
    Last Post: 11-17-2011, 12:49 PM
  2. Gui problem and stack overflow error
    By zaricpp in forum Advanced Java
    Replies: 5
    Last Post: 06-03-2011, 10:54 AM
  3. Replies: 1
    Last Post: 03-17-2010, 06:25 AM
  4. Stack Overflow work around?
    By Coukapecker in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 09:49 PM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 10:01 PM

Tags for this Thread

Posting Permissions

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