Results 1 to 3 of 3
Thread: Issues with sorting algorithm
- 03-26-2012, 02:56 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Issues with sorting algorithm
I'm trying to implement a sorting algorithm for my HairSalon class that sorts a HairSalon array in ascending order by price. The getPrice() method returns the price of the service for that element. During runtime immediately after inputting the data, I keep getting the error "java.lang.NullPointerException: Null", and I'm unsure as to why.
Here is the sorting part.
You fill each array manually, and then it sorts the array.Java Code:public static void priceAscend(HairSalon[] item, int size) { for (int k = 0; k < size - 1; k++) swapMinToFront (item, k, size - 1); } private static void swapMinToFront (HairSalon[] item, int start, int end) { int indexSmallest = start; for (int k = start + 1; k <= end; k++) { if (item[k].getPrice() < item[indexSmallest].getPrice()) indexSmallest = k; } HairSalon saved = item[start]; item[start] = item[indexSmallest]; item[indexSmallest] = saved; } //======================= } or alternatively public static void priceAscend(HairSalon[] par) { for(int x = 0; x < 6 - 1; x++) { int smallest = x; for(int y = 1; y < par.length - 1; y++) { if(par[y].getPrice() < par[y-1].getPrice()) { smallest = y; } } HairSalon saved = par[x]; par[x] = par[smallest]; par[smallest] = saved; } }
In case you need to look at the actual class, here it is.Java Code:public static void main (String[] args) { HairSalon[] salonList = new HairSalon[6]; for(int x = 0; x < 6; x++) { salonList[x].service = JOptionPane.showInputDialog("Hair Salon "+(x+1)+" service:"); salonList[x].price = Double.parseDouble(JOptionPane.showInputDialog("Hair Salon "+(x+1)+" price:")); salonList[x].minutes = Integer.parseInt(JOptionPane.showInputDialog("Hair Salon "+(x+1)+" minutes:")); } priceAscend(salonList, 6); String s = ""; for(int x = 0; x < 6; x++) { s += salonList[x].getPrice() + " "; } JOptionPane.showMessageDialog(null, s); }
Thanks in advance for any help with this issue!Java Code:public class HairSalon { public static String service; public static double price; public static int minutes; public void HairSalon(String serv, double cost, int mins) { service = serv; price = cost; minutes = mins; } public double getPrice() { return price; } public String getService() { return service; } public int getTime() { return minutes; } }Last edited by RAW-BERRY; 03-26-2012 at 03:48 PM.
- 03-26-2012, 03:02 PM #2
Re: Issues with sorting algorithm
Are you sure you're getting that error at compile-time? NullPointerExceptions are a runtime Exception, not a compile-time error. Also, what is the full stack trace for the Exception? What line is it occurring on?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-26-2012, 03:45 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Help with algorithm for sorting an array of Strings
By eIO in forum New To JavaReplies: 5Last Post: 07-11-2011, 01:40 PM -
Help with a sorting algorithm
By doobybug in forum New To JavaReplies: 2Last Post: 01-14-2011, 12:39 PM -
Algorithm help? :)
By Mirix in forum New To JavaReplies: 6Last Post: 05-24-2010, 02:08 AM -
Need some help in an algorithm
By ea09530 in forum New To JavaReplies: 3Last Post: 04-04-2010, 01:13 PM -
O(log n) algorithm help !!!!!!
By itseeker87 in forum New To JavaReplies: 8Last Post: 09-09-2008, 05:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks