Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2013
    Posts
    69
    Rep Power
    0

    Default How to resolve java.lang.StackOverFlowError when using a recursive method call?

    I have 3 Xml documents that look like this:

    model1:
    Java Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <model>
            <id>1</id>
            <nodes>
                <id>2</id>
                <stencil>TASK</stencil>
            </nodes>
        	<nodes>
                <id>3</id>
                <stencil>MODEL</stencil>
            </nodes>
        </model>
    model2:
    Java Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <model>
            <id>3</id>
            <nodes>
                <id>10</id>
                <stencil>TASK</stencil>
            </nodes>
        	<nodes>
                <id>11</id>
                <stencil>MODEL</stencil>
            </nodes>
        </model>
    model3:
    Java Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <model>
            <id>11</id>
            <nodes>
                <id>12</id>
                <stencil>TASK</stencil>
            </nodes>
        </model>
    I have to create another XML doc from the existing xml docs that looks like this:
    Java Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <model id="1">
            <task id="2">
            </task>
            <subModel id="3">
        	    <task id="10">
                </task>
        	    <subModel id="11">
        	        <task id="12">
                    </task>
                </subModel>
            </subModel>
        </model>
    So ... a subModel can have other subModel-s inside it and I have to create a recursive method to handle this

    I have used JAXB to unmarshal the given input XML file into Java object.
    Then I create new Java object and marshal it back to XML string.
    All this job is done as explained in:

    http://stackoverflow.com/questions/2...n-existing-one

    After unmarshalling/marshalling in the main class I have created this 3 methods:
    Java Code:
        public List<Task> extractTasks(Model model) {
            List<Task> tasks = new ArrayList<Task>();
            for (ModelNodes modelNodes : model.getNodes()) {
                if (modelNodes.getStencil().equals("TASK")) {
                    tasks.add(new Task(modelNodes.getId()));
                }
            }
            return tasks;
        }
        
        public List<SubModel> extractSubModels(Model model) {
            List<SubProcess> subModels = new ArrayList<SubProcess>();
            for (ModelNodes modelNodes : model.getNodes()) {
                if (modelNodes.getStencil().equals("MODEL")) {
                    Model subModel = loadModel(Integer.parseInt(modelNodes.getId())); //loads the respective model of the sub Model 
                    subModels.add(new SubModel(modelNodes.getId(), extractTasks(subModel), extractSubModels(subModel)));//recursive call to the extractSubModels method ...
                }
            }
            return subModels;	
        }
        	
        public Model extractModel(Model model) {
            Model p = new Model(model.getId(), extractTasks(model), extractSubModels(subModel)); 
            return p;
        }
    When the recursive call to the extractSubModels method is made, I get a **java.lang.StackOverFlowError** ...
    Can anyone help me out with this please?
    Last edited by blertadhimitri; 03-25-2014 at 10:54 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to resolve java.lang.StackOverFlowError when using a recursive method call?

    Post your question here if you still need help with it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to resolve java.lang.StackOverFlowError when using a recursive method call?

    Hi,

    have you tried to add debug information or simply debug the code? You do not have many iteration when you go through the XML so there shouldn't be many function calls.

    Stack Overflow is normally showing that your design is either wrong (because you have to many recursive calls) or that you simply made an error with your recursive call (e.g. not going into the scope as expected in your case!)
    But such a problem could easily be found with checking the parameters at the start of a function. Then you can see what content is checked.

    But I also agree to Norm: If you want help in here then ask the question in here.


    With kind regards,

    Konrad

  4. #4
    Join Date
    Jan 2013
    Posts
    69
    Rep Power
    0

    Default Re: How to resolve java.lang.StackOverFlowError when using a recursive method call?

    I just edited my question and posted it here ...

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: How to resolve java.lang.StackOverFlowError when using a recursive method call?

    Well put some System.out.println statements inside extractSubModels to see what it is continuously looping over. If you get a stackoverflow that probably means it is continuously getting the same model, which would make me suspect the validity of that ID value you are using in your code; with the little information I have I would suspect that the model you get with loadModel() based on that modelNodes.getId() is not what you expect it to be.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Join Date
    Jan 2013
    Posts
    69
    Rep Power
    0

    Default Re: How to resolve java.lang.StackOverFlowError when using a recursive method call?

    You are right. It happend that it was getting the same model ....

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: How to resolve java.lang.StackOverFlowError when using a recursive method call?

    Well there you go, the most simplistic form of reasoning and the most simplistic form of debugging (System.out.println() - the programmer's friend) were all that was needed to get a step further into solving it. Programming is hard, debugging is easy :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. java.lang.StackOverflowError Exception
    By Marcus in forum Web Frameworks
    Replies: 4
    Last Post: 08-24-2012, 11:02 PM
  2. java.lang.StackOverflowError???
    By remus19 in forum New To Java
    Replies: 6
    Last Post: 05-06-2011, 03:55 AM
  3. java.lang.StackOverflowError
    By malstryx in forum New To Java
    Replies: 9
    Last Post: 10-01-2008, 05:14 AM
  4. java.lang.StackoverflowError
    By ravisankarvivek in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 10:05 AM
  5. java.lang.StackOverflowError
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-24-2007, 10:54 AM

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
  •