Results 1 to 17 of 17
- 11-04-2011, 08:02 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Why is this getting an exception error?
PLEASE HELP ME!! can anyone tell me why I am getting an error in this. I just want to print out the values stored in the arraylist. I don't understand why this has to be so difficult.
import java.util.ArrayList;
public class MyWorld{
static ArrayList<Beepers> beeperList = new ArrayList<Beepers>();
public MyWorld(){ /* add walls and beepers here */
System.out.println("***MyWorld Contructor***");
beeperList.add(new Beepers (5,7,31));
}
public static void main(String args[]){
System.out.println("The values for Beepers are"+beeperList.get(0).getStreet());
}
/********** Inner Class "Beepers" **************/
public class Beepers{
/******** declare variables *************/
private int street, avenue, count;
public Beepers(int s, int a, int c){ // Beepers class constructor
System.out.println("*** Beepers Constructor***");
int street=s;
int avenue=a;
int count=c;
}
/************Beeper Class Methods ********************************/
public int getStreet(){
System.out.println("*** getStreet() ***");
return street;
}
public int getAvenue(){
return avenue;
}
public int getCount(){
return count;
}
}
/************************************************** **************************/
}Last edited by kyle_maddisson; 11-04-2011 at 08:14 AM.
- 11-04-2011, 08:16 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
What exception? On what line?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-04-2011, 08:49 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
When I run it it says that the size is the ArrayList size is zero, so I get an out of bounds error. Why is it not adding an entry in the list. It was working a bit ago. This is ridiculous.
- 11-04-2011, 09:02 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-04-2011, 09:41 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
How is that possible? I add a new Beeper to beeperList? Isnt' that an object in the List.
- 11-04-2011, 09:43 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
public MyWorld(){ /* add walls and beepers here */
System.out.println("***MyWorld Contructor***");
beeperList.add(new Beepers (5,7,31));
}
these lines don't assign an object in the list? If that's the case then I have no idea what is going on.
- 11-04-2011, 09:44 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-04-2011, 09:55 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
that line is in another class. So it should be ok.
- 11-04-2011, 10:13 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
Jos
Huh? You're running the main( ... ) method in the MyWorld class and the constructor of that class is the only location where a Beeper object is added to that (static) list so the main( ... ) method should construct a MyWorld object ... of course it is ok, and it is the only possibilty to add an element to the list withouth changing any other code.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-04-2011, 10:36 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
Ok let me try to explain this as best I can here. I don't know if you familiar with Karel++, but anyway it's got a robot that walks around a world composed of a grid of streets and avenues. The streets increase along the y-axis and avenues increase along the x-axis. The two axis are the boundaries of the world. The world also has walls and beepers in it, and I need to write a class that keeps track of these objects and have my robot class interact with it. For instance, if the robot picks beepers up from an intersection then I want to to be able to subtract a beeper from the world. Specifically, the object that keeps track of the number of beepers on that corner. I need to make a Beeper class, as well as a wall class, and store objects of these classes in an ArrayList, because there could be any number of piles of beepers and any number of walls. Also, if a robot puts a beeper down, I need to be able to add and object to the ArrayList. Now I have already done the robot class (called UrRobot) and that has no main() in it. I do not want a main() in the World class either because both of these classes are being used by another java program called UrRobotTest, which is the one that has main() in it, and is the file that I run first. This file constructs and UrRobot object, which in turn will construct a static World object. The reason the world is static is because if there are multiple robots in the world then the world info must be available to all robots.
I don't know if I can explain it any better than that. Anyway, I have successfully got all three files to work together as far as constructing the objects goes. As for this problem I don't know what the problem was, but I rebooted my computer and now it runs without the exception and adds the objects to the ArrayList. However now my problem is I can't get the information stored in the fields of the Beeper objects out of the ArrayList, and I'm about ready to throw my computer against the wall.
Here is the complete World class I have got so far. I have been successful in invoking the methods of the Beeper class, but they return 0, which is not correct, and if I use get(index) it returns what I think is a hash code. I want the actual integer that is held in the field of the Beeper class, but can't for the life of me figure out how to do it.
I need the integer back so I can increment or decrement accordingly, and also I need to compare the intersection the robot is on to intersections in ArrayList which I have called beeperList. I don't understand why this is so difficult to do. I have done the best to explain this. If you want to see the other files let me know. I would appreciate any help with this you can give me with this immensely, you have no idea how frustrated i am right now.
please help!
import java.util.ArrayList;
public class MyWorld{
/************** declare variables ****************/
static ArrayList<Beepers> beeperList = new ArrayList<Beepers>();
/*********** Constructor ************/
public MyWorld(){ /* add walls and beepers here */
beeperList.add(new Beepers (1,1,3));
}
/********** Inner Class "Beepers" **************/
public class Beepers{
/******** declare variables *************/
private int street, avenue, count;
public Beepers(int s, int a, int c){ // Beepers class constructor
int street=s;
int avenue=a;
int count=c;
}
/************Beeper Class Methods ********************************/
public int getStreet(){
return street;
}
public int getAvenue(){
return avenue;
}
public int getCount(){
return count;
}
public int incrementCount(){
count++;
return count;
}
public int decrementCount(){
count--;
return count;
}
public void LinkTest(){
System.out.println("*** Beepers *** ...link established");
}
}
/******* World methods ************/
public void LinkTest(){
System.out.println("*** MyWorld*** ...link established");
}
}
- 11-04-2011, 12:59 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
Start by implementing a toString() method in your Beeper class so you can see the actual Beeper objects instead of Beeper@509abfea (or similar).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-04-2011, 01:14 PM #12
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
I have already tried that. I put it as the last method in the Beepers class. Here is what I wrote for the toString() method:
public String toSting(){
return "St: " +street +" Ave: " + avenue +" Count "+count;
}
but it still gets zeros for the values, so how does that help me?
- 11-04-2011, 01:35 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
It helps you in such a way that it confirms that those member variables are still zero; which is understandable because you created local variables of the same name in your constructor and assigned the parameters to those local variables (leaving the member variables zero).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-04-2011, 01:54 PM #14
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
I don't suppose you could show me how to fix that?
- 11-04-2011, 02:35 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-04-2011, 02:49 PM #16
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: Why is this getting an exception error?
wow thank you it worked. I could have sworn i typed it in that way at first but the compiler gave me an error so I put the int's in front then it let me compile. I don't know man
- 11-04-2011, 03:17 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this getting an exception error?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Exception Error
By unexpert in forum New To JavaReplies: 1Last Post: 02-09-2011, 05:30 PM -
Exception Error
By Sotsiak in forum New To JavaReplies: 1Last Post: 10-22-2010, 10:14 PM -
exception error
By kira137 in forum New To JavaReplies: 5Last Post: 10-12-2009, 07:46 AM -
Exception error
By Rose88 in forum New To JavaReplies: 8Last Post: 07-06-2009, 10:22 PM -
JSF error+exception
By Peter in forum SWT / JFaceReplies: 1Last Post: 07-04-2007, 06:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks