Personal computing discussed

Moderators: SecretSquirrel, just brew it!

 
FireGryphon
Darth Gerbil
Topic Author
Posts: 7491
Joined: Sat Apr 24, 2004 7:53 pm
Location: the abyss into which you gaze

more Java: Vector out of bounds

Sat Jul 31, 2004 9:28 pm

this line of code:

if (fName == (monitorsVector.elementAt(i)).toString())

gives this error:

ArrayIndexOutOfBoundsException: 0>=0

Any idea how to fix that?
 
Yahoolian
Grand Gerbil Poohbah
Posts: 3577
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD
Contact:

Sat Jul 31, 2004 9:30 pm

First of all make sure i is valid.

Use .equals for comparing Strings.

== sees if the references are referring to the same object.
 
FireGryphon
Darth Gerbil
Topic Author
Posts: 7491
Joined: Sat Apr 24, 2004 7:53 pm
Location: the abyss into which you gaze

Sat Jul 31, 2004 10:22 pm

changed to:


for(int i = 0; i < monitorsVector.capacity(); i++)
{
if (fName.equalsIgnoreCase((monitorsVector.elementAt(i)).toString()))
return ((FileMonitor) monitorsVector.elementAt(i));
}


I still get the same error. Any further ideas?
 
daveagn
Gerbil XP
Posts: 468
Joined: Wed Jun 02, 2004 5:44 pm

Sat Jul 31, 2004 10:48 pm

Have you printed out the result of monitorsVector.capacity() to make sure it's returning the correct value?
 
FireGryphon
Darth Gerbil
Topic Author
Posts: 7491
Joined: Sat Apr 24, 2004 7:53 pm
Location: the abyss into which you gaze

Sat Jul 31, 2004 10:59 pm

daveagn wrote:
Have you printed out the result of monitorsVector.capacity() to make sure it's returning the correct value?


Yes, it prints out a bunch of "10"'s and then I have it exit.

By this point, there are no objects yet in the vector. Could it be that java crashes when it tries to change a null (or garbage) to a string? if so, how can i fix it?
 
Yahoolian
Grand Gerbil Poohbah
Posts: 3577
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD
Contact:

Sun Aug 01, 2004 10:06 am

Yes, it prints out a bunch of "10"'s and then I have it exit.

By this point, there are no objects yet in the vector.


Well if there aren't any objects in it, you want it to be returning 0, so that's your problem right there.

for(int i = 0; i < monitorsVector.capacity(); i++)
{
if (fName.equalsIgnoreCase((monitorsVector.elementAt(i)).toString()))
return ((FileMonitor) monitorsVector.elementAt(i));
}


Change .capacity() to .size()

Capacity is how much it can hold without enlarging, size is how much it currently holds.

I find the API Specs are very useful (choose the version of Java that you're using).

Could it be that java crashes when it tries to change a null (or garbage) to a string?

That would probably give you a null pointer exception, instead of what you're getting.

It looks like the array bounds checking is giving the error, which means the index is invalid. If Java lacked array bounds checking, it would probably give you a null pointer exception.

If an array has 0 objects, and you try to get from 0 - 9 of it, it will give you that ArrayIndexOutOfBounds exception.
 
FireGryphon
Darth Gerbil
Topic Author
Posts: 7491
Joined: Sat Apr 24, 2004 7:53 pm
Location: the abyss into which you gaze

Sun Aug 01, 2004 1:27 pm

Yahoolian: The reason I use .capacity() is because I want it to check every cell, empty or not, in the vector. Since Vector.addElement() appends objects to the end of the vector, vectors grow with predetermined sizes, and objects may be removed from the middle of the vector, it may be that a vector of capacity 10 has only three elements in it, with one element at the very end of the vector. If I do a search up to Vector.size(), it'll search through the first three elements of the vector and miss the ones later on.

One thing I don't get is why it crashes when it tries to read empty vector cells -- cells that exist, just have nothing in them.
 
Yahoolian
Grand Gerbil Poohbah
Posts: 3577
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD
Contact:

Sun Aug 01, 2004 1:37 pm

All right, what you want to do is use an iterator to go through the vector.

Interator i = monitorVector.iterator();
FileMonitor o;
while( i.hasNext() )
{
  o = (FileMonitor)i.next();
  if (fName.equalsIgnoreCase(o.toString())
    return o;
}


One thing I don't get is why it crashes when it tries to read empty vector cells -- cells that exist, just have nothing in them.

Well I think it makes sure something is in the cell before it tries to read it.
 
FireGryphon
Darth Gerbil
Topic Author
Posts: 7491
Joined: Sat Apr 24, 2004 7:53 pm
Location: the abyss into which you gaze

Sun Aug 01, 2004 4:07 pm

I implemented the code. It looks good. Now I have to figure out why none of the elements in my vector are found when I search for them.

EDIT:

See, my program starts out with nothing in the vector. So it skips that code, and calls my addMonitor() method. Later, it has to find the monitor again, but for some reason, after going through the code you supplied, it still doens't find the monitor. I'm guessing it's a problem with the code I've written.

searching... searching...
 
Yahoolian
Grand Gerbil Poohbah
Posts: 3577
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD
Contact:

Sun Aug 01, 2004 4:43 pm

What does the fName String contain?

What does the FileMonitor toString return?
 
dalamar70
Gerbil First Class
Posts: 100
Joined: Thu Aug 29, 2002 3:23 pm

Sun Aug 01, 2004 4:57 pm

FireGryphon wrote:
objects may be removed from the middle of the vector, it may be that a vector of capacity 10 has only three elements in it, with one element at the very end of the vector.


Vector will automatically handle that situation. When you remove something from a Vector, all the elements that came after it will appear to be "shifted."

One thing that might help is to play around with some simpler test programs. Here's a small one.

import java.util.Vector;

public class vector
{
  public static void printIntegerVector(Vector v)
  {
    System.out.println("Vector size: " + v.size());
    for (int i = 0; i < v.size(); i++)
      { System.out.println("Element " + i + ": " + v.get(i)); }
    System.out.println("");
  }

  public static void main(String args[])
  {
    Vector v = new Vector();

    // Vector with Integers 0, 1, 2, 3, 4
    for (int i = 0; i < 5; i++)
      { v.addElement(new Integer(i)); }
    printIntegerVector(v);

    // Removing the 0th element
    v.removeElementAt(0);
    printIntegerVector(v);
  }
}


The program puts a 0 in vector position #0, a 1 in position #1, etc. After you remove the element at position #0, you can still access position #0--but now it contains 1. So a Vector is a little bit more than just an array that can be resized.

Who is online

Users browsing this forum: No registered users and 1 guest