• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can Vectors contain Vectors?

 
Reid Hattaway
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a nice day.......
Yes, dear friend
It is possible to vector can take other vector.
bye
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 388
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic