Creating array from generic type objects?
Okay... I currently am in the process of creating a Radix Sort for this class that I'm taking. My whole problem is that I have created a generic type class called ADTQueue. I know that this class functions correctly. I now want to create an array of String type ADTQueue objects to use as "buckets" to throw objects from an original array of random numbers. So here is the actual probelm....
After I create my buckets, every time I try to use one of the ADTQueue buckets it says "java.lang.Nullpointerexception"
This is how I created it:
ADTQueue<String>[] buckets = new ADTQueue[10];
then when I try something like:
buckets[0].enqueue(someString);
I get the error of a null pointer, but I am pointing at the first ADTQueue object in the array at index 0.
If I create a bucket without an array it works correctly:
ADTQueue<String> aBucket = new ADTQueue();
aBucket.enqueue(someString);
System.out.println (aBucket.dequeue());
This then prints out the string correctly so I know the ADTQueue method in itself is not at fault...
**************************************…
So my question I suppose is:
How do I create an array using the generic type objects in string form?
Thanks so much for any help!
Re: Creating array from generic type objects?
If your class is a queue, it should probably be able to hold object itself without the help of an array.
ADTQueue<String>[] buckets = new ADTQueue[10];
Hmmm...that doesn't have the generic on the right side like it should.
ADTQueue<String>[] buckets = new ADTQueue<String>[10];
Also, both your queue and array will initially be empty.
You have to put stuff in the array or else it'll throw a Null Pointer Exception.
Re: Creating array from generic type objects?
Throwing numbers into a String array also may be problematic.
Re: Creating array from generic type objects?
ADTQueue<String> aBucket = new ADTQueue();
Not even sure why it let you get away with the line above as you forget the generic parameter on the right side there too.
Anyway, I think it's because you're calling a method when you don't have anything in the array or something like that.
Re: Creating array from generic type objects?
Quote:
Originally Posted by
AndrewMiller
ADTQueue<String>[] buckets = new ADTQueue[10];
then when I try something like:
buckets[0].enqueue(someString);
I get the error of a null pointer
Do you ever instantiate the values of the array you create?
eg
Code :
ADTQueue<String>[] buckets = new ADTQueue[10];
for ( int i = 0; i < 10; i++ ){
buckets[i] = new ADTQueue();
}