Results 1 to 17 of 17
  1. #1
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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 09:14 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why is this getting an exception error?

    What exception? On what line?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why is this getting an exception error?

    Quote Originally Posted by kyle_maddisson View Post
    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 eny in the list. It was working a bit ago. This is ridiculous.
    Your main method tries to print element #0 in an empty list; (no object was constructed yet, so nothing was added to the list). It isn't ridiculous, it's just your mistake ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

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

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

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

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why is this getting an exception error?

    Quote Originally Posted by kyle_maddisson View Post
    How is that possible? I add a new Beeper to beeperList? Isnt' that an object in the List.
    You add an element to that list in the constructor of a MyWorld object but you're never constructing such object; you could add a "new MyWorld();" as the first line of the body of your main( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Why is this getting an exception error?

    that line is in another class. So it should be ok.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why is this getting an exception error?

    Quote Originally Posted by kyle_maddisson View Post
    that line is in another class. So it should be ok.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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?

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why is this getting an exception error?

    Quote Originally Posted by kyle_maddisson View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Why is this getting an exception error?

    I don't suppose you could show me how to fix that?

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why is this getting an exception error?

    Quote Originally Posted by kyle_maddisson View Post
    I don't suppose you could show me how to fix that?
    Don't assign to local variables but to the member variables instead:

    Java Code:
    public Beepers(int s, int a, int c){ // Beepers class constructor
       System.out.println("*** Beepers Constructor***");
       street=s;
       avenue=a;
       count=c;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why is this getting an exception error?

    Quote Originally Posted by kyle_maddisson View Post
    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
    You're welcome of course and don't swear, it doesn't help you one bit, nor does throwing your computer out of the window or setting it on fire ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Exception Error
    By unexpert in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 06:30 PM
  2. Exception Error
    By Sotsiak in forum New To Java
    Replies: 1
    Last Post: 10-22-2010, 11:14 PM
  3. exception error
    By kira137 in forum New To Java
    Replies: 5
    Last Post: 10-12-2009, 08:46 AM
  4. Exception error
    By Rose88 in forum New To Java
    Replies: 8
    Last Post: 07-06-2009, 11:22 PM
  5. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 07:29 AM

Posting Permissions

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