# Issues with sorting algorithm

• 03-26-2012, 02:56 PM
RAW-BERRY
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.
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;         }     }```
You fill each array manually, and then it sorts the array.
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.
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!
• 03-26-2012, 03:02 PM
KevinWorkman
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?
• 03-26-2012, 03:45 PM
RAW-BERRY
Re: Issues with sorting algorithm
Yes, sorry. It is a runtime error which occurs immediately after inputing all appropriate data.