Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why does this happen

1 view
Skip to first unread message

Laura & Larry

unread,
Sep 20, 2002, 3:24:48 PM9/20/02
to
I'm making a vector from a vector using this code

public MarkList makeSubList(Student s)
{
Vector v = new Vector();
Enumeration e = markVec.elements();
while (e.hasMoreElements())
{
Mark mark = (Mark)e.nextElement();
if (s.getID().equals(mark.getSID()))
{
v.addElement(mark);
}
}
return new MarkList(v);
}

in a few cases it creates a vector of 8 or 9 objects, which is correct. My
problem is that after it reaches a point where one of the students is
missing (10 students in original list) I get arry errors. eg:
A vector of nine goes like this. the first 4 students qualify and the 5th
doesn't, the remain students do qualify. So my vector size is 9. scrolling
through my students starting at index0 is fine until i get to my fifth
student i then get this error:

Starting appletviewer for C:\au\Assignments\comp268\tme2\Registry.htm
java.lang.ArrayIndexOutOfBoundsException: 5 >= 3
at java.util.Vector.elementAt(Vector.java:427)
at MarkList.getGrade(MarkList.java:84)
at Registry.displayStud(Registry.java:442)
at Registry.handleStudForB(Registry.java:312)
at Registry.actionPerformed(Registry.java:199)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3526)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.ja
va:190)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)

it does show my 5th person though, i continue geting errors until i getr
back to index 0 or index 4

this driving me up the wall!!

thanks
--larry

Lothar Kimmeringer

unread,
Sep 20, 2002, 4:46:15 PM9/20/02
to
Laura & Larry schrieb:

>
> I'm making a vector from a vector using this code
>
> public MarkList makeSubList(Student s)
[...]

> }
>
> Starting appletviewer for C:\au\Assignments\comp268\tme2\Registry.htm
> java.lang.ArrayIndexOutOfBoundsException: 5 >= 3
> at java.util.Vector.elementAt(Vector.java:427)
> at MarkList.getGrade(MarkList.java:84)
> at Registry.displayStud(Registry.java:442)
> at Registry.handleStudForB(Registry.java:312)
[...]

Are you sure, that the exception occurs while accessing the
vector containing marks? You're speaking about a vector of
students. Are there two vectors and you focus on the wrong
one?

If not, then you should post more code belonging to the
exception stack-trace. Print out the elements of the
vector before the exception occur (and the size of the
vector as well).

You provide too less information to do something different
than guessing.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: spam...@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!

A Bag Of Memes

unread,
Sep 20, 2002, 5:13:06 PM9/20/02
to

"Laura & Larry" <lese...@shaw.ca> wrote in message
news:4CKi9.377268$Ag2.16...@news2.calgary.shaw.ca...

>
> Starting appletviewer for C:\au\Assignments\comp268\tme2\Registry.htm
> java.lang.ArrayIndexOutOfBoundsException: 5 >= 3
> at java.util.Vector.elementAt(Vector.java:427)
> at MarkList.getGrade(MarkList.java:84)

What's going on at line 84 in MarkList? Are you removing elements from the
Vector while looping through it, perhaps? The error indicates you're trying
to access element 5 of a 3 element Vector.

Laura & Larry

unread,
Sep 20, 2002, 5:22:35 PM9/20/02
to
this is my button code:

public void handleStudForB ()
{
if (mStud)
{
sIndex = (sIndex + 1) % studList.size();
if (studMode)
displayStudent();
else
displayStud();
}
}

so then im accessing displayStud

public void displayStud ()
{
studList = studMaster.makeSubList(markListOfC);
student = studList.getStud(sIndex);
display9();
markListOfS = markMaster.makeSubList(student);
ncc = markListOfS.size();
txtNCC.setText("" + ncc);
sg = markListOfS.getGrade(sIndex);
sag = markListOfS.getAveGrade();
txtStudAG.setText("" + (round1(sag)));
txtStudGr.setText("" + (round1(sg)));
}

studList calls this one

public MarkList makeSubList(Course c)


{
Vector v = new Vector();
Enumeration e = markVec.elements();
while (e.hasMoreElements())
{
Mark mark = (Mark)e.nextElement();

if (c.getID().equals(mark.getCID()))
{
v.addElement(mark);
}
}
return new MarkList(v);
}


--larry

"Lothar Kimmeringer" <news2...@kimmeringer.de> wrote in message
news:3D8B8917...@kimmeringer.de...

Lothar Kimmeringer

unread,
Sep 20, 2002, 5:48:58 PM9/20/02
to
Laura & Larry schrieb:

>
> this is my button code:

[...]

Are the variables you're using defined globally?

in handleStudForB:


sIndex = (sIndex + 1) % studList.size();

Will sIndex be resetted to zero anywhere in the code? If not,
the first vector that contains lesser elemens than the previous
one will result into an ArrayIndexOutOfBoundsException.

Still it's not possible to do more than guessing. The exception
occurs in getGrade and you haven't posted it yet.

Laura & Larry

unread,
Sep 20, 2002, 6:11:47 PM9/20/02
to
yes my vars are globally defined
public double getGrade ( int i )
{
return ((Mark)markVec.elementAt(i)).getGrade();
}

-larry


"Lothar Kimmeringer" <news2...@kimmeringer.de> wrote in message

news:3D8B97CA...@kimmeringer.de...

A Bag Of Memes

unread,
Sep 20, 2002, 6:50:03 PM9/20/02
to

"Laura & Larry" <lese...@shaw.ca> wrote in message
news:D2Ni9.363107$v53.17...@news3.calgary.shaw.ca...

> yes my vars are globally defined
> public double getGrade ( int i )
> {
> return ((Mark)markVec.elementAt(i)).getGrade();
> }

Your logic is questionable. You constrain sIndex to the size of studList,
then use it to reference an element in markVec. It looks like markVec is
smaller than studList.

Laura & Larry

unread,
Sep 20, 2002, 11:27:09 PM9/20/02
to
markVec contains an object for each course each student have taken

-larry


"A Bag Of Memes" <a@b.c> wrote in message
news:vCNi9.502433$q53.16...@twister.austin.rr.com...

A Bag Of Memes

unread,
Sep 21, 2002, 1:21:14 AM9/21/02
to

"Laura & Larry" <lese...@shaw.ca> wrote in message
news:hGRi9.378947$Ag2.16...@news2.calgary.shaw.ca...

> markVec contains an object for each course each student have taken

Then why are you using sIndex, which I assume means "student index", or
perhaps currentStudentNumber, to pry an element out of markVec?

It might be easier to understand the code if you asked Student or Course for
the grade. Maybe something like:

double grade = aStudent.getGrade(course);

0 new messages