Results 1 to 10 of 10
  1. #1
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Bubblesort Issue within Method

    Hello, again. I'm getting an exception message when trying to pass strings to this method:

    Java Code:
    import java.util.Scanner;
    
    public class Search {
    
    	public static void main(String[] args) {
    	
    		Scanner sc = new Scanner(System.in);
    		Scanner bc = new Scanner(System.in);
    		String[] firstName = new String[25];
    		String[] lastName = new String[25];
    		int nameCount = 0;
    		
    		System.out.print("How many names would you like to enter? ");
    		nameCount = bc.nextInt();
    		System.out.print("\n");
    		
    		for (int i = 0; i < nameCount; i++) {
    			System.out.println("Enter first name: ");
    			firstName[i] = sc.nextLine();
    		}
    		sortName(nameCount, firstName);
    	
    	
    	}
    	public static void sortName(int nCount, String[] fName) {
    		String tmpStr;
    		for (int i = 0; i < nCount; i++) {
    			if (fName[i].compareTo(fName[i + 1]) > 0) {
    				tmpStr = fName[i];
    				fName[i] = fName[i + 1];
    				fName[i + 1] = tmpStr;
    				}
    			}
    		for (int i = 0; i < nCount; i++)
    			System.out.println(fName[i]);
    		}
    	
    	
    	}
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at java.lang.String.compareTo(String.java:1167)
    	at Search.sortName(Search.java:28)
    	at Search.main(Search.java:21)
    I'm simply trying to arrange these first names into alphabetical order. I also tried to add ".length - 1" to the end of "nCount", but it says a delimiter isn't allowed. All help is appreciated!
    Last edited by burrish; 09-26-2011 at 09:26 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: Bubblesort Issue within Method

    Quote Originally Posted by burrish View Post
    Hello, again. I'm getting an exception message when trying to pass strings to this method:
    And now we have to guess what Exception has been thrown at you and at which line?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Bubblesort Issue within Method

    ....apologies. The exception has been added.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: Bubblesort Issue within Method

    Since your posted code does not have 28 lines, can you tell
    Which variable on line 28 is null? If you can't tell by looking, add a println call to print out the values of all the variables on that line.


    add ".length - 1" to the end of "nCount", but it says a delimiter isn't allowed. All help is appreciated!
    nCount is a primitive NOT an object reference. There are no attributes for a primitive.
    You could use .length with fName.
    Last edited by Norm; 09-26-2011 at 09:25 PM.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Bubblesort Issue within Method

    Look at how you are accessing the array in the loop where the exception is thrown (assuming the line numbers line up between your exception and code). For example, what happens when i is equal to nCount-1? Has the object at the array position nCount-1+1 been instantiated? Also note your algorithm will not fully sort the array.

  6. #6
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Bubblesort Issue within Method

    Thanks to all of you for the replies, so far. I appreciate the clarification, Norm.

    No, it doesn't look like nCount-1+1 has been instantiated for use, so I'm going to adjust the numbers to try and make it work. I've seen that most bubblesort algorithms use two for-loops, but I didn't think it was necessary in my method since I'm comparing rows of single strings.

  7. #7
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Continuing with Array Sorting

    Guys, I'm about to pull my hair with these "NullPointer" exceptions. For simplicity's sake, I decided to forgo bubblesort and use the Array class to do a really short test-program to test the sorting functionality. Here's the code:

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Sorter {
    
    	public static void main(String[] args) {
    		
    			Scanner sc = new Scanner(System.in);
    			String[] firstName = new String[25];
    			// String[] firstName = {"Brandon", "Kim", "Aaron", "Larry", "Gary"};
    			
    			System.out.println("Enter five names: ");
    			
    			for (int i = 0; i < 5; i++) {
    				firstName[i] = sc.nextLine();
    				}
    				
    			Arrays.sort(firstName);
    			
    			for (int i = 0; i < 5; i++) 
     				System.out.println(firstName[i]);
    			
    			
    			
    	}
    
    
    
    }
    If you run this code and enter the five names, it throws out the following exception:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at java.lang.String.compareTo(String.java:1167)
    	at java.lang.String.compareTo(String.java:92)
    	at java.util.Arrays.mergeSort(Arrays.java:1144)
    	at java.util.Arrays.mergeSort(Arrays.java:1155)
    	at java.util.Arrays.mergeSort(Arrays.java:1155)
    	at java.util.Arrays.sort(Arrays.java:1079)
    	at Sorter.main(Sorter.java:18)
    If you comment out the prompt for names, the for-loop that scans the names, and then you uncomment the initialized array, the sort method works exactly as it should. What in the HECK am I doing wrong??

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: Bubblesort Issue within Method

    The array has 25 slots and you are putting data in 5 of them.
    What does the sort() method do with slots with null values?

  9. #9
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Bubblesort Issue within Method

    Quote Originally Posted by Norm View Post
    What does the sort() method do with slots with null values?
    It throws out an exception because there's nothing there. :) Thanks!

    I guess I was expecting it to dynamically adjust to however many names the user decides to enter as long as I had a long enough array.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: Bubblesort Issue within Method

    Go back to the API doc and see if there are other overrides for sort that will do what you want.

Similar Threads

  1. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  2. Issue in method anonymous class in GWT
    By ankit01 in forum GWT
    Replies: 0
    Last Post: 05-16-2011, 11:25 AM
  3. Question with bubbleSort()
    By Metastar in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 05:55 PM
  4. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM
  5. Die Bubblesort
    By N3VRMND in forum New To Java
    Replies: 2
    Last Post: 11-06-2009, 03:44 PM

Posting Permissions

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