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

Can some one...

0 views
Skip to first unread message

Laura & Larry

unread,
Sep 1, 2002, 5:56:40 PM9/1/02
to
I'm trying to write a program for a course I'm taking and have got 3/4 of it
working, the last 1/4 I'm getting a useless error description (or at least
to me):
java.lang.ArrayIndexOutOfBoundsException: 4 >= 3
at java.util.Vector.elementAt(Vector.java:427)
at MarkList.getGrade(MarkList.java:89)
at Registry.displayStud(Registry.java:435)
at Registry.displayCourse(Registry.java:419)
at Registry.handleCourForB(Registry.java:395)
at Registry.actionPerformed(Registry.java:202)
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)


I know my array is going out of bounds, but in this program one half of the
code is almost a mirror of the other.

If someone would permit me to e-mail them my code I'll explain excatly what
is going on and where my problem is. I'd post code but Im unsure of what
code to post.

My e-mail address is lese...@shaw.ca if someone is willing to help.

thanks
Larry Seymour


Chris Smith

unread,
Sep 1, 2002, 6:47:41 PM9/1/02
to
[This followup was posted to comp.lang.java.programmer and a copy was
sent to the cited author.]

Laura & Larry wrote ...


> I'm trying to write a program for a course I'm taking and have got 3/4 of it
> working, the last 1/4 I'm getting a useless error description (or at least
> to me):
> java.lang.ArrayIndexOutOfBoundsException: 4 >= 3
> at java.util.Vector.elementAt(Vector.java:427)
> at MarkList.getGrade(MarkList.java:89)

...

> If someone would permit me to e-mail them my code I'll explain excatly what
> is going on and where my problem is. I'd post code but Im unsure of what
> code to post.

Welcome to the newsgroup. Patricia has written some very good advice for
debugging code and made it available on her web site at
http://www.concentric.net/~pats/debug/

We'd love to help you out further, but no one can do so without seeing
your code. Offers of code by e-mail are really not appropriate, since
this is a public forum.

What you want to do is simplify the aspects of your application that
aren't working, and remove additional complexities one by one (make a
copy of your original first, of course!) until you've simplified things
enough that you either understand what's going on or have a more concrete
question to ask about why that code acts the way it does. Then you post
the code to the newsgroup.

> My e-mail address is lese...@shaw.ca if someone is willing to help.

It's also not appropriate, in a public forum, to ask that people answer
in ways that only you can see. We'd rather discuss the matter publicly
on the newsgroup. For now, though, in case you aren't continuing to
monitor the group, I've CC'ed you on this message.

Chris Smith

Marshall Spight

unread,
Sep 1, 2002, 7:12:54 PM9/1/02
to
I second everything Chris Smith said. I'd suggest particular attention to
Patricia's essay; a good rule of thumb I use is to listen closely
to anything Patricia says. (Chris too, for that matter.)

I'd also like to add a little bit:


"Laura & Larry" <lese...@shaw.ca> wrote in message news:s2wc9.246633$f05.13...@news1.calgary.shaw.ca...


> I'm trying to write a program for a course I'm taking and have got 3/4 of it
> working, the last 1/4 I'm getting a useless error description (or at least
> to me):

I can help you interpret this so it won't seem quite so "useless."

This means that you have an array of three elements, and you are trying
to get the element at index 4, which is actually two past the end. (Indicies
start at 0.) The array in question is contained inside a Vector.

> java.lang.ArrayIndexOutOfBoundsException: 4 >= 3
> at java.util.Vector.elementAt(Vector.java:427)


> at MarkList.getGrade(MarkList.java:89)

This is the specific line of code that's causing the problem. If you look at
MarkList.java, on line 89, you'll see a call to the method "elementAt". You
are passing this method a 4, but the Vector only contains 3 items.

So you have to ask yourself, *should* it contain more elements? If so,
why does it only have 3? Alternatively, why did the code ask for an
element with such a high index?


> at Registry.displayStud(Registry.java:435)
> at Registry.displayCourse(Registry.java:419)
> at Registry.handleCourForB(Registry.java:395)
> at Registry.actionPerformed(Registry.java:202)
> 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)

This is the rest of the stack trace.

HTH.


Marshall

Laura & Larry

unread,
Sep 1, 2002, 7:50:47 PM9/1/02
to
Marshall:
Thanks for your explaination of the error, I led me to what my problem was.
I have many vectors and I was using the wrong one at that time.

Chris:
Thanks for the link to Patricia's site. Going back bit by bit until my
error occured and with Marshall's explaination fixed it for me.

Next time I'll go back step by step then post my code.

Thanks for now

-larry

"Marshall Spight" <msp...@dnai.com> wrote in message
news:W9xc9.94149$_91....@rwcrnsc51.ops.asp.att.net...

Laura & Larry

unread,
Sep 1, 2002, 7:54:25 PM9/1/02
to
A different question....
Why can't a text field accept an integer instead of a string?
Why must we must we converty it to a string?

--Larry


"Marshall Spight" <msp...@dnai.com> wrote in message
news:W9xc9.94149$_91....@rwcrnsc51.ops.asp.att.net...

Michiel Konstapel

unread,
Sep 1, 2002, 8:23:23 PM9/1/02
to
"Laura & Larry" <lese...@shaw.ca> wrote in message
news:RMxc9.245049$Ag2.12...@news2.calgary.shaw.ca...

> A different question....
> Why can't a text field accept an integer instead of a string?
> Why must we must we converty it to a string?

Because it's a *text* field :)
In Java, there is almost no automatic conversion between types. String
concatenation ("some string " + somethingElse) is about the only one.
For converting something to a String, String.valueOf() is the most
straightforward method. For fancier stuff, look at the NumberFormat classes
in java.text.*.
HTH,
Michiel


Laura & Larry

unread,
Sep 1, 2002, 8:34:07 PM9/1/02
to
I know I can do
"" + integer
that works as well

"Michiel Konstapel" <a...@me.nl> wrote in message
news:%byc9.457303$Ma.38...@amsnews02.chello.com...

Michiel Konstapel

unread,
Sep 1, 2002, 8:36:32 PM9/1/02
to
"Laura & Larry" <lese...@shaw.ca> wrote in message
news:3myc9.245186$Ag2.12...@news2.calgary.shaw.ca...

> I know I can do
> "" + integer
> that works as well

Yup, but most people consider that a rather nasty hack.
http://jinx.swiki.net/77 discusses some of the alternatives.
Michiel

Jon A. Cruz

unread,
Sep 1, 2002, 8:56:19 PM9/1/02
to
Laura & Larry wrote:

> I know I can do
> "" + integer
> that works as well

String.valueOf( integer ) is better.

--
Jon A. Cruz

What's this? It's almost as though the fates were tempting me to...
Bad fates. Baaad... Naughty fates...

Laura & Larry

unread,
Sep 1, 2002, 9:43:14 PM9/1/02
to
So I didn't get it...
Here is the code:

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


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);
}

public double getAveGrade ()
{
double avg = 0;
Enumeration e = markVec.elements();
while (e.hasMoreElements())
avg += ((Mark)e.nextElement()).getGrade();
if (avg == 0)
{
return avg;
}
else
{
double ns = size();
return avg = avg / ns;
}
}

public int size()
{
return markVec.size();
}

public double getGrade ( int i )
{
return ((Mark)markVec.elementAt(i)).getGrade();
}

When I run the program without sg = markListOfS.getGrade(sIndex);
all my vector sizes check out. as soon as i add it in and run it i get an
array out of bounds:
java.lang.ArrayIndexOutOfBoundsException: 5 >= 3


at java.util.Vector.elementAt(Vector.java:427)
at MarkList.getGrade(MarkList.java:89)

......

my vector size changes from 5 to 3.

do i need to add something???

thanks in advance
-Larry

Patricia Shanahan

unread,
Sep 2, 2002, 5:00:58 PM9/2/02
to

Laura & Larry wrote:
...

> When I run the program without sg = markListOfS.getGrade(sIndex);
> all my vector sizes check out. as soon as i add it in and run it i get an
> array out of bounds:
> java.lang.ArrayIndexOutOfBoundsException: 5 >= 3
> at java.util.Vector.elementAt(Vector.java:427)
> at MarkList.getGrade(MarkList.java:89)
> ......
>
> my vector size changes from 5 to 3.
>
> do i need to add something???

Yes, you need to add some elements to the Vector, or use its setSize
method. Check the API documentation for Vector's elementAt method.

Patricia

0 new messages