File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can Vectors contain Vectors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Vectors contain Vectors?" Watch "Can Vectors contain Vectors?" New topic
Author

Can Vectors contain Vectors?

Reid Hattaway
Greenhorn

Joined: Jul 15, 2001
Posts: 7
First off, the big picture: I have a program in which i need to read in many character files (about 25 right now). There are two main classes in this program. One class of the program sends a series of search key Strings to the second class. I created a second class to facilitate the management of knowing which files to search and to do the actually searching. This second class tells the first class if the search key String is valid.
I currently have a function that uses the args[] string to read each file and it returns a Vector full of String objects. I pass this Vector to a function(named addDataFile) of the second class, (the File Managing class), which should take each Vector it receives (named list) and add it to or insert it into another Vector(named datafiles). This second Vector(datafiles) is a data member of the File Managing class. I have a second function of the File Managing class that handles retrieving the correct Vector from (datafiles).
I run into problems when I try to add a Vector(list) to the (datafiles) Vector. Everytime I add or insert a Vector(list), i get a null pointer exception during runtime. This occurs even if I can print out the contents of (list) using a FOR loop before I try to insert of add (list) to (datafiles). I have no compilation errors.
Here's the relevant code:
public void addDataFile(Vector list)
{
String tmp = (String) list.get(0);
int wordSize = tmp.length();
datafiles.insertElementAt(list, wordSize);
}
/*I get the same null pointer exception during runtime if I use datafile.add(wordSize, list) instead of insertElementAt.*/
It may be helpful to know that datafiles starts out completely empty, but that I do use datafiles.setSize(17) during the construction of the FileManager object that calls addDataFile(). wordSize will never be greater than or equal to 17.
Why do i get the null pointer exception?
Can I use Vector inside Vectors?
kaival patel
Greenhorn

Joined: Jul 11, 2001
Posts: 5
Have a nice day.......
Yes, dear friend
It is possible to vector can take other vector.
bye
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You can use vector inside a vector.
But with your code the following change will help:
before issuing any get() or elementAt() on a vector, please check whether the vector is not null:
if ( list!=null)
{
....
}
Hope this helps.
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi,
vector can hold objects. everything descends from object so you can put in everithing but primitive datatypes (they have to be wrapped in Integer objects, Boolean onjects and so on..)

karl
Reid Hattaway
Greenhorn

Joined: Jul 15, 2001
Posts: 7
I've done a little more testing and tried some experimentation. I have discovered that it really didn't matter what type of object i tried to add, insert, or set in datafiles, I would still get the null pointer exception.
I tried Dip Chak's suggestion for testing the Vector list. list, as I had always believed, is not null. I mentioned that I could access it with a print function. HOWEVER, when i tested datafiles itself to see if it was valid, it always tested NULL. So, I think that I'm messing up somewhere in the declarations of my class constructor. Here's the code I think is relevant:
public class BoggleFileManager
{
private Vector dataFiles;

public void BoggleFileManager()
{
dataFiles = new Vector();
}
}
LOL. As soon as i pasted this code, i saw my problem. Because i incorrectly used void in the constructor, dataFiles = new Vector() is never called when i declare a BoggleFileManager object, and thus the reference for datafiles is never allocated although the declaration of datafiles itself is valid. I took out void and the program worked like a charm. I'm surprised this did not generate a compilation error. Jesus, i feel like and idiot. Thanks Dip Chack!!
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Originally posted by Reid Hattaway:
Jesus, i feel like and idiot.

Get used to it. I get that feeling ALL THE TIME.

[This message has been edited by Cindy Glass (edited July 30, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Vectors contain Vectors?