Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating array from generic type objects?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Creating array from generic type objects?

    Throwing numbers into a String array also may be problematic.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating array from generic type objects?

    Quote Originally Posted by AndrewMiller View Post
    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
    ADTQueue<String>[] buckets = new ADTQueue[10];
    for ( int i = 0; i < 10; i++ ){
        buckets[i] = new ADTQueue();
    }

Similar Threads

  1. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  2. How to create a pointer type of effect for objects
    By zelalem in forum Object Oriented Programming
    Replies: 6
    Last Post: October 20th, 2010, 02:53 AM
  3. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM