Results 1 to 5 of 5
  1. #1
    Domo230 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Null pointer exception

    Hi I am using eclipse to create a list of numbers and do a bucket sort on them.
    However in the bucket sort method I am getting a null pointer exception. AFAIK null pointer exceptions happen when you do stuff like try get the length of something that has been declared null but I have been looking over my code and it doesnt seem to be doing something like that.
    Any help would be appreciated.




    Java Code:
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class SortUtils {
    
    	
    	//sorts bucket and returns sorted list of numbers
    
    
    	static ArrayList bucketSort(ArrayList<Integer> array){
    		//creates arraylist that will hold buckets
    		ArrayList[] buckets =new ArrayList[10];
    		int digit_result;
    		ArrayList<Integer> list = null;
    		for(int i=0;i<=array.size();i++){
    
    			digit_result=getDigit(array.get(i),1);
    			//If the space in the arraylist is empty, create a new //ArrayList that serves as the 
    			//bucket and has the value in the arraylist added to it
    			if(buckets[i]==null){
    				//creates list that will serve as one of the buckets
    				list = new ArrayList<Integer>(1);
    				//adds value to the bucket
    				list.add(array.get(i));
    				//adds bucket to the arraylist
    				buckets[digit_result].add(list);
    			}
    
    			else{
    				list.add(array.get(i));
    				buckets[digit_result].add(list);
    			}
    
    		}
    
    		for(int i=0;i<=10;i++){
    
    			if(buckets[i]!=null){
    
    				for(int j=0;j<=buckets[i].size();j++){
    					selectionSort(buckets[i]);
    				}
    
    			}
    
    
    		}
    		return null;
    	}
    
    
    	//gets digit
    	private static int getDigit(int value, int i) {
    		int pow = (int) Math.pow(10, i-1);
    		return (value / pow) % 10;
    	}
    
    	//sorts out in the bucket
    	private static void selectionSort(List<Integer> A) {
    		int minIndex = -1;
    		//seems to just get the same thing as a.size() and set as //minindex 
    		for (int j=0; j < A.size(); j++) {
    			minIndex = j;
    
    			for (int k=j+1; k < A.size(); k++) {
    				if (A.get(minIndex) > A.get(k)) {
    					minIndex = k;
    				}
    			}
    			if (minIndex != j) {
    				int temp = A.get(j);
    				A.set(j, A.get(minIndex));
    				A.set(minIndex, temp);
    			}
    		}
    	}
    
    
    }

    And here is the test class with the main method
    Java Code:
    import java.util.ArrayList;
    
    
    public class test {
    
    	
    	public static void main(String[] args) {
    		int digit_result;
    		ArrayList<Integer> testarray =new ArrayList<Integer>();
    		testarray.add(10);
    		System.out.println(testarray);
    		testarray.add(20);
    		testarray.add(15);
    		System.out.println(testarray);
    		SortUtils.bucketSort(testarray);
    		System.out.println(testarray);
    	}
    
    }

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Java Code:
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class SortUtils {
    
    	
    	//sorts bucket and returns sorted list of numbers
    
    
    	static ArrayList bucketSort(ArrayList<Integer> array){
    		//creates arraylist that will hold buckets
    		ArrayList[] buckets =new ArrayList[10];
    		int digit_result;
    		ArrayList<Integer> list = null;
    		for(int i=0;i<=array.size();i++){
    
    			digit_result=getDigit(array.get(i),1);
    			//If the space in the arraylist is empty, create a new //ArrayList that serves as the 
    			//bucket and has the value in the arraylist added to it
    			if(buckets[i]==null){
    				//creates list that will serve as one of the buckets
    				list = new ArrayList<Integer>(1);
    				//adds value to the bucket
    				list.add(array.get(i));
    				//adds bucket to the arraylist
    				buckets[digit_result].add(list);
    			}
    
    			else{
    				[COLOR="Red"]list.add(array.get(i));[/COLOR]
    				buckets[digit_result].add(list);
    			}
    
    		}
    
    		for(int i=0;i<=10;i++){
    
    			if(buckets[i]!=null){
    
    				for(int j=0;j<=buckets[i].size();j++){
    					selectionSort(buckets[i]);
    				}
    
    			}
    
    
    		}
    		return null;
    	}
    
    
    	//gets digit
    	private static int getDigit(int value, int i) {
    		int pow = (int) Math.pow(10, i-1);
    		return (value / pow) % 10;
    	}
    
    	//sorts out in the bucket
    	private static void selectionSort(List<Integer> A) {
    		int minIndex = -1;
    		//seems to just get the same thing as a.size() and set as //minindex 
    		for (int j=0; j < A.size(); j++) {
    			minIndex = j;
    
    			for (int k=j+1; k < A.size(); k++) {
    				if (A.get(minIndex) > A.get(k)) {
    					minIndex = k;
    				}
    			}
    			if (minIndex != j) {
    				int temp = A.get(j);
    				A.set(j, A.get(minIndex));
    				A.set(minIndex, temp);
    			}
    		}
    	}
    
    
    }
    At the point highlighted above, during the first loop (only if this piece of code is reached) you should get a null pointer exception because the variable 'list' is still null

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    for(int i=0;i<=array.size();i++){...}
    Your for-loops tend to run from 0 to size in stead of size-1. this gives an IndexOutOfBounds or NullPointerException.
    A few more comments:
    I've run your code and saw that you forget a few times to parameterize your arraylists (ArrayList<Integer>).
    Also it is confusing to call a list "array". (especially if you also have an array of lists)
    I don't know why you use the 1 (initial size) in
    list = new ArrayList<Integer>(1);
    It's not wrong but confusing (for me).

    I haven't checked the algorithm as such until now.

    (It goes wrong before the "else" mr_guy marked.)
    Last edited by Jodokus; 02-26-2011 at 08:44 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    in the bucket sort method I am getting a null pointer exception. AFAIK null pointer exceptions happen when you do stuff like try get the length of something that has been declared null but I have been looking over my code and it doesnt seem to be doing something like that.

    The compiler's message will pinpoint the location of the problem. Post the whole message if you can't understand it.

    (another place is if you call a method on a variable which is null. eg foo[bar].whatever() where the bar slot in the foo array has not been assigned a value.)
    Last edited by pbrockway2; 02-26-2011 at 10:04 PM.

  5. #5
    Domo230 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for pointing out that it was being caused by list being null.
    Thanks for the help, I eventually got it working.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 04:41 PM
  3. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 PM
  4. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  5. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 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
  •