Hi, I'm a new java user, working on a brute-force knights tour algorithm in linkedlist. The move function worked perfectly (for a while), I mean, the pointer could move forward, or backward if a wrong path was chosen. But all of a sudden, it returned me a stackoverflow error. i'm really confused, can anyone help me? (I'm pretty sure that everything else was done correctly except for the following function) I know my code is kinda tedious but since it is mostly repeating, it would not take you too long to read it. I really appreciate your help and your patience!!

p.s position1 to position8 are eight possible moves of the knight.

public void move(Node newNode)
    {   
        System.out.println(counter);
        newNode.setMove(counter);
        display2();
        System.out.println();

        if (counter<64)
        {
            if (newNode.getRight()!=null&& newNode.getDown()!=null&&newNode.getDown().getDown()!=null&&newNode.isPosition1()==false&&newNode.getDown().getDown().getRight().isOccupied()==false)
            {
                counter++;
                newNode.setPosition1(true);
                newNode.getDown().getDown().getRight().setOccupied(true);
                newNode.setNext(newNode.getDown().getDown().getRight());
                newNode.getDown().getDown().getRight().setPrev(newNode);

                move(newNode.getNext());

            }

            else if (newNode.getLeft()!=null&& newNode.getDown()!=null&&newNode.getDown().getDown()!=null&&newNode.isPosition2()==false&&newNode.getDown().getDown().getLeft().isOccupied()==false)
            {
                counter++;
                newNode.setPosition2(true);
                newNode.getDown().getDown().getLeft().setOccupied(true);
                newNode.setNext(newNode.getDown().getDown().getLeft());
                newNode.getDown().getDown().getLeft().setPrev(newNode);

                move(newNode.getNext());

            }

            else if (newNode.getDown()!=null&&newNode.getLeft()!=null&&newNode.getLeft().getLeft()!=null&&newNode.isPosition3()==false&&newNode.getLeft().getLeft().getDown().isOccupied()==false)
            {
                counter++;
                newNode.setPosition3(true);
                newNode.getLeft().getLeft().getDown().setOccupied(true);
                newNode.setNext(newNode.getLeft().getLeft().getDown());
                newNode.getLeft().getLeft().getDown().setPrev(newNode);

                move(newNode.getNext());

            }

            else if (newNode.getUp()!=null&&newNode.getLeft()!=null&&newNode.getLeft().getLeft()!=null&&newNode.isPosition4()==false&&newNode.getLeft().getLeft().getUp().isOccupied()==false)
            {
                counter++;
                newNode.setPosition4(true);
                newNode.getLeft().getLeft().getUp().setOccupied(true);
                newNode.setNext(newNode.getLeft().getLeft().getUp());
                newNode.getLeft().getLeft().getUp().setPrev(newNode);

                move(newNode.getNext());        


            }

            else if (newNode.getLeft()!=null&&newNode.getUp()!=null&&newNode.getUp().getUp()!=null&&newNode.isPosition5()==false&&newNode.getUp().getUp().getLeft().isOccupied()==false)
            {
                counter++;
                newNode.setPosition5(true);
                newNode.getUp().getUp().getLeft().setOccupied(true);
                newNode.setNext(newNode.getUp().getUp().getLeft());
                newNode.getUp().getUp().getLeft().setPrev(newNode);

                move(newNode.getNext());

            }

            else if (newNode.getRight()!=null&&newNode.getUp()!=null&&newNode.getUp().getUp()!=null&&newNode.isPosition6()==false&&newNode.getUp().getUp().getRight().isOccupied()==false)
            {
                counter++;
                newNode.setPosition6(true);
                newNode.getUp().getUp().getRight().setOccupied(true);
                newNode.setNext(newNode.getUp().getUp().getRight());
                newNode.getUp().getUp().getRight().setPrev(newNode);

                move(newNode.getNext());

            }

            else if (newNode.getUp()!=null&&newNode.getRight()!=null&&newNode.getRight().getRight()!=null&&newNode.isPosition7()==false&&newNode.getRight().getRight().getUp().isOccupied()==false)
            {
                counter++;
                newNode.setPosition7(true);
                newNode.getRight().getRight().getUp().setOccupied(true);
                newNode.setNext(newNode.getRight().getRight().getUp());
                newNode.getRight().getRight().getUp().setPrev(newNode);

                move(newNode.getNext());            

            }

            else if (newNode.getDown()!=null&&newNode.getRight()!=null&&newNode.getRight().getRight()!=null&&newNode.isPosition8()==false&&newNode.getRight().getRight().getDown().isOccupied()==false)
            {
                counter++;
                newNode.setPosition8(true);
                newNode.getRight().getRight().getDown().setOccupied(true);
                newNode.setNext(newNode.getRight().getRight().getDown());
                newNode.getRight().getRight().getDown().setPrev(newNode);

                move(newNode.getNext());

            }
            else 
            {
                Node node=new Node();
                if (newNode.getUp()!=null)
                {
                    newNode.getUp().setDown(node);
                    node.setUp(newNode.getUp());
                }
                if (newNode.getDown()!=null)
                {
                    newNode.getDown().setUp(node);
                    node.setDown(newNode.getDown());
                }
                if (newNode.getRight()!=null)
                {
                        newNode.getRight().setLeft(node);
                        node.setRight(newNode.getRight());
                }
                if (newNode.getLeft()!=null)
                {
                    newNode.getLeft().setRight(node);
                    node.setLeft(newNode.getLeft());
                }

                counter--;
                newNode.setOccupied(false);

                if (newNode.getPrev()==null)
                    System.out.println("athis");
                if (newNode.getPrev()!=null)
                    move(newNode.getPrev());
            }       
        }
    }

Stack overflow is almost certainly caused by you recursively calling a method from inside itself (or from inside a method that it calls). The error message should also tell you the line number where it happened, so that's where you start looking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.