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.
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

