This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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?
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.
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
posted
0
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!!