Results 1 to 10 of 10
Thread: Bubblesort Issue within Method
- 09-26-2011, 09:13 PM #1
Member
- Join Date
- Sep 2011
- Location
- Texas
- Posts
- 17
- Rep Power
- 0
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]); } }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!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)
Last edited by burrish; 09-26-2011 at 09:26 PM.
- 09-26-2011, 09:16 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 09-26-2011, 09:20 PM #3
Member
- Join Date
- Sep 2011
- Location
- Texas
- Posts
- 17
- Rep Power
- 0
Re: Bubblesort Issue within Method
....apologies. The exception has been added.
- 09-26-2011, 09:23 PM #4
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.
nCount is a primitive NOT an object reference. There are no attributes for a primitive.add ".length - 1" to the end of "nCount", but it says a delimiter isn't allowed. All help is appreciated!
You could use .length with fName.Last edited by Norm; 09-26-2011 at 09:25 PM.
- 09-26-2011, 09:32 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
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.
- 09-26-2011, 09:44 PM #6
Member
- Join Date
- Sep 2011
- Location
- Texas
- Posts
- 17
- Rep Power
- 0
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.
- 09-27-2011, 02:18 AM #7
Member
- Join Date
- Sep 2011
- Location
- Texas
- Posts
- 17
- Rep Power
- 0
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:
If you run this code and enter the five names, it throws out the following exception: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 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??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)
- 09-27-2011, 02:24 AM #8
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?
- 09-27-2011, 02:36 AM #9
Member
- Join Date
- Sep 2011
- Location
- Texas
- Posts
- 17
- Rep Power
- 0
- 09-27-2011, 02:39 AM #10
Similar Threads
-
Could not find or load main class BubbleSort.class
By blaqkout in forum New To JavaReplies: 5Last Post: 09-12-2011, 07:54 PM -
Issue in method anonymous class in GWT
By ankit01 in forum GWTReplies: 0Last Post: 05-16-2011, 11:25 AM -
Question with bubbleSort()
By Metastar in forum New To JavaReplies: 1Last Post: 09-13-2010, 05:55 PM -
Turning Recursion Method into Iterative method
By mattakuevan in forum New To JavaReplies: 9Last Post: 06-15-2010, 06:46 AM -
Die Bubblesort
By N3VRMND in forum New To JavaReplies: 2Last Post: 11-06-2009, 03:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks