PDA

View Full Version : getting a runtime exception, dont know why


uniquity
05-13-2009, 06:49 AM
for my 102 class i have to create a LinkedList class that has to pass a given test driver. i've passed most things in the test but the test is getting stuck on a runtime exception that is thrown for one of the constructors. the constructor code is below, it takes in a List and adds its elements to the end of the ArrayList. if you need the test driver as well, that can be provided

public LinkedList( java.util.List<E> list )
{
head = null;
size = 0;

if( list.isEmpty() )
{
}
else
{
for( int ctr = 0; ctr < list.size(); ctr++ )
{
this.add( list.get( ctr ) );
size++;
}
}
}

The test driver also specifies that its an ArrayOutOfBoundsException, but i dont see where i would be going out of bounds with my code. thanks in advance for any help

Gox
05-14-2009, 01:53 AM
Could you provide some addition details? The Runtime ArrayOutOfBoundsException will most likely provide some additional details such as the File and Line Number associated with the exception. Futhermore, in your code you have a call to this.add which could possibly be the source of the problem, however you haven't provided the code for the add method. Did you write the add method yourself or is your LinkedList class extending another class which already has an add method?

uniquity
05-14-2009, 02:40 AM
:rolleyes: sorry bout the lack of details...ok, looks like i just didnt think clearly about this problem and i may be able to figure it out myself
what happened was the test driver my instructor gave me was throwing the array out of bounds exception because it was trying to access something from the linked list...which was never added
and after u made the remark on my add method (which is coded later in the same file <- i know, stupid :mad: ) i think i realized where i'm going wrong hehe...i'll post later after i make some modifications and let u know if i fixed it or not...thanks for the post!

uniquity
05-14-2009, 02:47 AM
alritey, yea i fixed it...i did the constructor after i made the add method and i dunno what i was thinking at the time, but i tried using the add method and apparently that doesnt work hehe