Results 1 to 6 of 6

Thread: Stack Overflow?

  1. #1
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Stack Overflow?

    I was wondering if somebody could help me with a problem I am having with my code. I am getting a stack overflow error, and it appears like something recursive is happening, but I don't see what is recursive with my code. I am trying to add an object to an arraylist in another class. I am trying to do it from the constructor of the object class I am constructing. Basically I am constructing the object, then trying to pass that object to an arraylist in another object. As far as I can tell the constructor should only be called once, so why it is acting recursive, I don't know. The particular object in question is an UrRobot, and I'm trying to store it in a World object's arraylist. Sure would appreciate some help with this one.

    Here are the files listed in the order they are used. Keep in mind these are separate files/ classes. I don't know if that matters or not.
    Java Code:
    public class BasicUrRobotTest{
    	public static void main(String args[]){
    
    	UrRobot karel = new UrRobot(1,2,3,2);  //THE OBJECT I'M TRYING TO STORE IN WORLD'S ARRAYLIST
    
    
    	karel.w.display();
    	karel.display();
    	karel.putBeeper();
    	karel.putBeeper();
    	karel.display();
    	karel.w.display();
    	}
    }
    ----------------------------------------------------------------------------------------------------------------
    import java.util.ArrayList;
    
    public class UrRobot{
    	/*********** declare variables for UrRobot class **************/
    
    	static MyWorld w = new MyWorld();
    	 int street;
    	 int avenue;
    	 int direction;
    	 int beepers;
    	String status=("on");
    	public String compass[]={"not used","North", "South", "East", "West"};
    
    
    				/********* constructor ***********/
        public UrRobot(int street, int avenue, int direction, int beepers){
    		this.street=street;
    		this.avenue=avenue;
    		this.direction=direction;
    		this.beepers=beepers;
    		this.status=status;
    		w.robotList.add(new UrRobot(street, avenue, direction, beepers));   // STACK OVERFLOW 
    	}
    /*****************************************************
                         Method definitons
    ******************************************************/
    
    		/********** display() ********************/
    
    	public void display(){ System.out.println("\n* UrRobot display() *\n");
    		if(status.equals("on")){
    			System.out.println("Robot: ");
    			System.out.print("        Location: ");
    			System.out.print(street +" st, ");
    			System.out.println( avenue +" ave");
    			System.out.println("       Direction: " +compass[direction] );
    			System.out.println("  Beepers in bag: " +beepers);
    			System.out.println("          On/Off: " +status);
    		}
    		else{
    		 	System.out.println("**Error** display() message sent to robot that was turned off****\n");
    		}
    	}
    
    		/****************** turnLeft ***************/
    
    	public void turnLeft(){
    		System.out.println("\n* UrRobot turnleft()*");
    		turnLeft:
    		if (status.equals("on")){
    			if(direction==1){
    				direction=4;
    				break turnLeft;
    			}
    			if (direction==2){
    				direction=3;
    				break turnLeft;
    			}
    			if (direction==3){
    				direction=1;
    				break turnLeft;
    			}
    			if (direction==4){
    				direction=2;
    				break turnLeft;
    			}
    		}
    		else
    			System.out.print("***Error*** turnleft() message sent to a robot that was turned off\n");
    	}
    
    		/*********** putBeeper ***************/
    
    	public void putBeeper(){
    		System.out.println("\n* UrRobot putBeeper() *");
    
    		if(status.equals("on")){
    			if (beepers > 0){
    				beepers=beepers-1;
    				System.out.println("UrRobot beeper bag= "+beepers);
    				w.putBeeper(street, avenue);
    
    			}
    			else{
    		 		System.out.println("*** Error Shutoff *** putBeeper() message sent to robot that had no beepers in bag\n");
    		 		status="off";
    		 	}
    		}
    		else
    			System.out.print("**Error** putBeeper() message sent to a robot that was turned off\n");
    	}
    
    
    		/***************** pickBeeper ***********/
    
    	public void pickBeeper(){
    		System.out.println("\n* UrRobot pickBeeper()*");
    		if(status.equals("on")){
    			if(w.foundBeepers(street, avenue)){
    				beepers=beepers+1;
    				System.out.println("UrRobot beeper bag= "+beepers);
    				w.pickBeeper(street, avenue);
    			}
    			else{
    				System.out.println("*** Error Shutoff *** pickBeeper() message sent to a robot with no beepers on that corner \n");
    				status = "off";
    			}
    		}
    		else
    			System.out.print("**Error** pickBeeper() message sent to a robot that was turned off\n");
    	}
    
    		/****************** move() **************/
    
    	public void move(){
    		System.out.println("\n* UrRobot move()*");
    		if(status.equals("on")){
    		    if(!w.hitWall(street, avenue, direction)){
    		    	if (direction==1)
    					street=street+1;
    				if (direction==2 && street<2){
    					System.out.println("*** Error Shutoff *** robot ran into a wall \n");
    					status="off";
    				}
    				else{
    					 if(direction==2)
    					 street=street-1;
    				}
    				if (direction==3)
    					avenue=avenue+1;
    				if (direction==4 && avenue<2){
    					System.out.println("*** Error Shutoff *** robot ran into a wall \n");
    					status="off";
    				}
    				else if(direction==4)
    					avenue=avenue-1;
    		    }
    		    else{
    		    	System.out.println("*** Error Shutoff *** robot ran into a wall");
    		    	status="off";
    		    }
    		}
    		else
    			System.out.print("*** Error Shutoff *** move() message sent to a robot that was turned off\n");
    	}
    
    		/***************** turnOff ***************/
    
    	public void turnOff(){
    		System.out.println("* UrRobotturnoff() *");
    		if(status.equals("on")){
    			status=("off");
    		}
    		else
    			System.out.print("** Error ** turnOff() message sent to a robot that was turned off\n");
    	}
    
    	public String toString()
    		{
    		return "St: " +street +", " +"Ave: " + avenue +", "+"Dir: "+compass[direction] +", " +"Bag: "+beepers +", "+status;
    	}
        static public void displayWorld()
            {
                    w.display();
    	}
    }
    ------------------------------------------------------------------------------------------------------------------------------------
    
    import java.util.ArrayList;
    
    public class MyWorld{
    
    /************** declare variables ****************/
    	ArrayList<Beepers> beeperList = new ArrayList<Beepers>();
    	ArrayList<UrRobot> robotList = new ArrayList<UrRobot>();
    	Walls[] wallsNorth = new Walls[1];
    	Walls[] wallsSouth = new Walls[1];
    	Walls[] wallsEast = new Walls[1];
    	Walls[] wallsWest = new Walls[1];
    	boolean found=false;
    
    /*********** Constructor ************/
    public MyWorld(){ /* add walls and beepers here */
    	System.out.println("*** MyWorld Constructor ***\n");
    	// beeperList.add(new Beepers (10,101,100));
    	// beeperList.add(new Beepers (1,2,0));
    
    	wallsNorth[0]=new Walls(1,2);
    	wallsSouth[0]=new Walls(1,2);
    	wallsEast[0]=new Walls(1,2);
    	wallsWest[0]=new Walls(1,2);
    
    
    }
    
    
    	/******* World methods ************/
    
    	// searches ArrayList for beeperpile on robots current corner
    	public boolean foundBeepers(int UrRobotStreet, int UrRobotAvenue){
    	//	System.out.println("* w.foundBeepers *");
    		for(Beepers e : beeperList){
    			System.out.println("("+e.getStreet()+","+e.getAvenue()+")");
    			if (e.getStreet()==UrRobotStreet && e.getAvenue()==UrRobotAvenue && e.getCount()>0){
    				found = true;
    				System.out.println("* match found *");
    			}
    			else{
    				 found = false;
    			}
    		}
    		return found;
    	}
    
    		// decrements beeper-pile count
    	public void pickBeeper(int UrRobotStreet, int UrRobotAvenue){ //System.out.println("* w.pickBeeper() *");
    		if(foundBeepers(UrRobotStreet, UrRobotAvenue)){
    			for(Beepers e : beeperList){
    				if (e.getStreet()==UrRobotStreet && e.getAvenue()==UrRobotAvenue && e.getCount()>0)
    					e.decrementCount();
    				}
    			}
    		return ;
    		}
    
    	// increments beeper-pile count
    	public void putBeeper(int UrRobotStreet, int UrRobotAvenue){ //System.out.println("* w.putBeeper() *");
    		if(foundBeepers(UrRobotStreet, UrRobotAvenue)){
    				for(Beepers e : beeperList){
    					if (e.getStreet()==UrRobotStreet && e.getAvenue()==UrRobotAvenue)
    						e.incrementCount();
    				}
    		}
    		if(!foundBeepers(UrRobotStreet, UrRobotAvenue)){
    			beeperList.add(new Beepers (UrRobotStreet, UrRobotAvenue, 1));
    
    		}
    		return ;
    	}
    
    	// displays all World object information
    	public void display(){
    			for(Beepers e : beeperList){
                               System.out.println( "Beeper street: " + e.getStreet() +
                                                   " " +  "avenue: " + e.getAvenue() +
                                                   " " +  "count: " + e.getCount());
                }
    			for(Walls e : wallsNorth){
                    System.out.println( "Wall street: " + e.getStreet() +
                                                   " " +  "avenue: " + e.getAvenue());
                }
                for(Walls e : wallsSouth){
                    System.out.println( "Wall street: " + e.getStreet() +
                                         " " +  "avenue: " + e.getAvenue());
                }
                for(Walls e : wallsEast){
                    System.out.println( "Wall street: " + e.getStreet() +
                                       " " +  "avenue: " + e.getAvenue());
                }
                for(Walls e : wallsWest){
                    System.out.println( "Wall street: " + e.getStreet() +
                                        " " +  "avenue: " + e.getAvenue());
                }
    	}
    
    	// checks to see if robot hit wall
    	public boolean hitWall(int UrRobotStreet, int UrRobotAvenue, int UrRobotDirection){
    
    		boolean collision = false;
    
    		//System.out.println("World checking for wall collisions");
    
    		if (UrRobotDirection==1){
    			for(Walls e : wallsNorth){
    				if(e.getStreet()==UrRobotStreet && e.getAvenue()==UrRobotAvenue){
    					collision=true;
    				}
    			}
    		}
    		if (UrRobotDirection==2){
    			for(Walls e : wallsSouth){
    				if(e.getStreet()==UrRobotStreet && e.getAvenue()==UrRobotAvenue){
    					collision=true;
    				}
    			}
    		}
    		if (UrRobotDirection==3){
    			for(Walls e : wallsEast){
    				if(e.getStreet()==UrRobotStreet && e.getAvenue()==UrRobotAvenue){
    					collision=true;
    				}
    			}
    		}
    		if (UrRobotDirection==4){
    			for(Walls e : wallsWest){
    				if(e.getStreet()==UrRobotStreet && e.getAvenue()==UrRobotAvenue){
    					collision=true;
    				}
    			}
    		}
    		return collision;
    	}
    }

    SORRY ABOUT THE INDENTATION, IT DOESN'T WANT TO KEEP MY SPACING WHEN I POST
    Last edited by JosAH; 11-17-2011 at 09:41 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,288
    Blog Entries
    7
    Rep Power
    24

    Default Re: Stack Overflow?

    Please, this is just too much code to plough through; try to come up with a small(er) example that shows the behaviour you described (and preferably make it compilable so we can test it).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,712
    Rep Power
    14

    Default Re: Stack Overflow?

    Java Code:
    public UrRobot(int street, int avenue, int direction, int beepers){
        this.street=street;
        this.avenue=avenue;
        this.direction=direction;
        this.beepers=beepers;
        this.status=status;
        w.robotList.add(new UrRobot(street, avenue, direction, beepers));
    }
    Basically I am constructing the object, then trying to pass that object to an arraylist
    But you aren't doing that: you are passing a new object to the list. Importantly, you are constructing a new object within the constructor which is what causes the stack overflow.

  4. #4
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: Stack Overflow?

    What? I thought it is constructed in BasicUrRobotTest. I thought UrRobot karel=new UrRobot(1,2,0) is what constructed it. I just want to send the paramaters to the arrayList in World? My reasoning being that the UrRobot constructor received the paramaters to construct the object. Then I just wanted to send that robot to the arrayList. Where else would I do that?

  5. #5
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: Stack Overflow?

    I'm sorry I cannot seem to shorten this anymore than it already is. If I do I just get errors. Each file works together and if omit anything, it can't find...., enumerator expected, etc. I tried to explain what lines I am having trouble with. If you look at the comments, it says where the problem is and what the error is I am getting on that line. As you can probably tell, I am not a very good programmer yet, but I feel I could be with practice and some help understanding what is going on. Perhaps I could just attach the files, would that make things easier? Oh never mind, who would have thought that a .java file would be an invalid file on a Java forum
    Last edited by kyle_maddisson; 11-17-2011 at 12:11 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,288
    Blog Entries
    7
    Rep Power
    24

    Default Re: Stack Overflow?

    Quote Originally Posted by kyle_maddisson View Post
    I'm sorry I cannot seem to shorten this anymore than it already is. If I do I just get errors. Each file works together and if omit anything, it can't find...., enumerator expected, etc. I tried to explain what lines I am having trouble with. If you look at the comments, it says where the problem is and what the error is I am getting on that line. As you can probably tell, I am not a very good programmer yet, but I feel I could be with practice and some help understanding what is going on. Perhaps I could just attach the files, would that make things easier? Oh never mind, who would have thought that a .java file would be an invalid file on a Java forum
    Pbrockway2 already told you were the error is: in the constructor of your robot class, you're trying to construct another robot and add that one to a list; but the other robot also tries to construct another robot in its constructor (it's the same constructor code, called recursively) and add another new one to the list, ad infinitum; that's why the stack overflows.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Gui problem and stack overflow error
    By zaricpp in forum Advanced Java
    Replies: 5
    Last Post: 06-03-2011, 09:54 AM
  2. Preventing stack overflow?
    By bobocheez in forum New To Java
    Replies: 11
    Last Post: 01-05-2011, 06:50 PM
  3. Stack Overflow work around?
    By Coukapecker in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 09:49 PM
  4. Java Stack Overflow?
    By fullmetaljacket in forum New To Java
    Replies: 0
    Last Post: 05-19-2009, 07:49 PM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 09:01 PM

Posting Permissions

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