# Issues with sorting algorithm

• April 30th, 2012, 10:57 PM
RAWBERRY
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 at the line "priceAscend(salonList, 6);", I keep getting the error "java.lang.NullPointerException: Null", and I'm unsure as to why.

Here is the sorting part.
Java Code:

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; } //======================= }```

You fill each array manually, and then it sorts the array.
Java Code:

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); }```
In case you need to look at the actual class, here it is.
Java Code:

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; } }```
Thanks in advance for any help with this issue!
• May 1st, 2012, 12:39 AM
Gigggas
Re: Issues with sorting algorithm
Your HairSalon class has declared all its variables as static objects. This would mean each HairSalon object in the array is referencing the same service, price, and minute variables. Remove the static keyword, and then try the program again.

Hope that helps!
• May 1st, 2012, 12:57 AM
pbrockway2
Re: Issues with sorting algorithm
If fixing (*) the static modifier doesn't resolve the problem, post the whole runtime stack trace. It contains information about which line the exception occured on which is invaluable.

-----

As far as "static" is concerned fixing==removing. Almost always. Resist the temptation to make things static in an attempt to keep the compiler quiet. It is your friend, compiler messages are always helpful.
• May 1st, 2012, 03:52 PM
RAWBERRY
Re: Issues with sorting algorithm
So, I've removed the static portion of the variables for my HairSalon class, and now I'm getting this error as soon as I attempt to enter any data into the first element of the salon list.
Code :

```java.lang.NullPointerException at SortSalon.main(SortSalon.java:8)```
I hope this is the runtime stack trace that pbrockway2 was asking for.

My HairSalon class now looks like this:
Code :

```public class HairSalon { public String service; public double price; public int minutes;   public 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; } }```

Please let me know if anything else is needed.

EDIT: Manually filling the array has shown that the actual algorithm used to sort the elements by price works, which is all I needed. However, I would still like to figure out why filling in the array during runtime isn't working. Hopefully I can figure it out myself before anyone else needs to stress themselves.

EDIT2: I figured it out ^_^ I wasn't actually declaring a new HairSalon class in each element, and was simply trying to modify a variable that didn't actually exist yet. So instead of:
Code :

```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:")); }```

I am now effectively using:
Code :

```for(int x = 0; x < 6; x++) { salonList[x] = new HairSalon(JOptionPane.showInputDialog("Hair Salon "+(x+1)+" service:"), Double.parseDouble(JOptionPane.showInputDialog("Hair Salon "+(x+1)+" price:")), Integer.parseInt(JOptionPane.showInputDialog("Hair Salon "+(x+1)+"minutes:"))); }```

Thanks for all of the help! Also, if a mod wants to rename this post title to something that more accurately reflects the problem, that would probably be better for people searching for a solution to a similar issue.
• May 1st, 2012, 04:20 PM
Norm
Re: Issues with sorting algorithm
Quote:

java.lang.NullPointerException
at SortSalon.main(SortSalon.java:8)
The error occurred at line 8 in the SortSalon class. What is the code for that class?
Has the salonList array been given values? Besides defining the array, you must assign a value to each element in the array.
• May 1st, 2012, 04:28 PM
aussiemcgr
Re: Issues with sorting algorithm
Which line is line 8?

Also, unrelated to your problem, but good to know for the future, I noticed you are sending the array as well as the size of the array, as two different arguments, to your priceAscend(...) method. I assume you don't know how to dynamically get the size of an array. The call: salonList.length would return the length (or size) of salonList, so in your case, that statement would return 6. Just something extremely handy to know for the future.
• May 1st, 2012, 05:13 PM
RAWBERRY
Re: Issues with sorting algorithm
Quote:

Originally Posted by Norm
The error occurred at line 8 in the SortSalon class. What is the code for that class?
Has the salonList array been given values? Besides defining the array, you must assign a value to each element in the array.

The SalonList array had not been given values, and that was in fact my problem! I was figuring this out right at the time of your posting, glad to have the solution confirmed. Thanks!

Quote:

Originally Posted by aussiemcgr
Which line is line 8?

Also, unrelated to your problem, but good to know for the future, I noticed you are sending the array as well as the size of the array, as two different arguments, to your priceAscend(...) method. I assume you don't know how to dynamically get the size of an array. The call: salonList.length would return the length (or size) of salonList, so in your case, that statement would return 6. Just something extremely handy to know for the future.

Thanks for the info! I normally would use it, and I always try to look at all available methods for the default java classes and identify useful ones, but I'm taking an online class right now, and it's easier on my teacher if I go ahead and do my work with what is given in the book.