Results 1 to 7 of 7
  1. #1
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default IndexOutOfBoundsException

    Well i have e f**k off project in for thursday and i have a little runtime error that goes as such :-

    notifyPassengerArrival(passengers.get(0));

    on this line i want to return the first element in the array (a Passenger object) and pass it to the notifyPassengerArrival method, but i keep getting an IndexOutOfBoundsException.

    I have confirmed that the array is not empty by using a println, so there is definetly an object at 0 in the array.

    I just dont get it!

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Can you show as your get(int index) method?
    freedom exists in the world of ideas

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Hi aldo1987,

    Welcome to our community. :)

    Yes, put some code here we can check/test. You say that, it is confirmed using println(), so what is the output? May be after that array content can be changed.

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Please don't you curse words, they just make you sound incompetent. If you post some code then we can help you.
    My IP address is 127.0.0.1

  5. #5
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    public void act()
    {
    //ADDED ENTIRE ACT FUNCTIONALITY
    if(destinations.isEmpty() == true && pickups.isEmpty() == true)
    {
    incrementIdleCount();
    }

    else
    {
    chooseTargetLocation();
    Location target = getTargetLocation();
    if(target != null) {
    // Find where to move to next.
    Location next = getLocation().nextLocation(target);
    setLocation(next);
    if(next.equals(target))
    {
    if(target == pickups.get(0))
    {
    notifyPickupArrival();
    }
    else if(target == destinations.get(0))
    {
    System.out.println(passengers.get(0));
    notifyPassengerArrival(passengers.get(0));
    offloadPassenger();
    }


    }
    }

    }


    }

    this is the method my problem appears in, The result of my println is "Passenger travelling from location 2,28 to location 18,21", in accordance with the toString method of the passenger class.

  6. #6
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Congratulation's for maintaining the ability to write Unmaintainable code. isEmpty = true is extremely redundant. Thats like saying is 1 = 1. I mean come on. Second thing I have absolutely know idea what this code does. Your passing in magic numbers into other methods that I can't see. Also please put your code in the code blocks next time it makes it more readable.
    My IP address is 127.0.0.1

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    yes aldo, your question is not clear. And some coding styles are pointless, as Zosden says,

    Java Code:
    if(destinations.isEmpty() == true && pickups.isEmpty() == true)
    no real mean of this. It's legal to use, but not good to use. isEmpty() always return a boolean value. So why did you comparing it again.

Posting Permissions

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