Results 1 to 8 of 8
  1. #1
    resspopv is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Null Pointer Exception in Radix Sort

    Hello,
    I am in the process of implementing the radix sort and have run into a problem with a null pointer exception. The error occurs when I run the program and it is at around line 17, array[p] = (queue[p].dequeue().toString());. The queue class is implemented correctly and is referenced based. The GenerateData class is also implemented correctly. Any help on why I get a null pointer exception at runtime would be greatly appreciated.



    Java Code:
    public static void radixSort(String[] array, int n, int d){
            ADTQueue<String>[] queue = new ADTQueue[n];
            for (int k = 0; k < n; k++){
                queue[k] = new ADTQueue();
            }
            for (int i = d-1; i >=0; i--){
                for (int j = 0; j < n; j++){
                    queue[j].clear();
                }
                for (int index = 0; index < n; index++){
                    char digit = (array[index].charAt(i));
                    int dig = Integer.parseInt(String.valueOf(digit));
    
                    queue[dig].enqueue(array[index]);
                    }
                    for (int p = 0; p < n; p++){
                        array[p] = (queue[p].dequeue()).toString();
                    }
                }
        }
    
        public static void main(String[] args){
            String[] array = GenerateData.getData(10);
            int n = array.length;
            int d;
            String temp = array[0];
            
            for (int j = 1; j < n; j++){
                if(array[j].compareTo(temp) > 0){
                    temp = array[j];
                }
            }
            
            d = temp.length();
    
            System.out.println("Origianl Data");
            displayArray(array);
    
            //selectionSort(array, n);
            //insertionSort(array, n);
            radixSort(array, n, d);
    
            System.out.println("\nSorted Data");
            displayArray(array);
    
        }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Could be a conflict with an array index. Arrays are zero indexing, so check the indexes first of all. Then check values of it.

    If you could post the complete error message we can comment on it easily.

  3. #3
    resspopv is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    I will look into the indexing and see if I messed something up. The full error message is:

    Exception in thread "main" java.lang.NullPointerException
    at SortArrayAll.radixSort<SortArrayAll.java:22>
    at SortArrayAll.main<SortArrayAll.java:46>

    The line numbers are a little off on here compared to my program but 22 is: array[p] = (queue[p].dequeue()).toString(); and 46 is: radixSort(array, n, d); in the main method.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Before this,

    Java Code:
    array[p] = (queue[p].dequeue()).toString();
    print the value of p. Then you can easily find the index which cause this problem.

  5. #5
    resspopv is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok, this is very weird. When I print out p the see where the null pointer exception is happening it seems like it depends on the data and is definitely not indexing right for some reason. When I run it and print out p sometimes it errors when p = 0 and other times it errors later but p will = like 0, 0, 1, 1, 2 and then error. I do not know why that is happening because p should be 0,1,2,3, etc. no matter what. Any ideas why this is happening?

  6. #6
    resspopv is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Disregard my previous post. I accidentally put two println's in when I was testing when array[p] would error. It does index correctly (0,1,2,3,4,etc.). I think I'm starting to figure out my problem. If the original data is like 0,1,3,4 it will error at array[2] because in the queue(bucket) there is no data at position 2 because there was no number 2. I just need to figure out how to get around that and I think I will be fine.

  7. #7
    resspopv is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    I figured it out. I just added a simple while (!queue[p].isEmpty()). Thank you for your help.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You are welcome, and I couldn't log into the forum last two days. So I haven't chance to looking into your thread.

    However thanks for marking the thread solved.

Similar Threads

  1. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  2. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  3. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •