# Thread: Issues with sorting algorithm

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.
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;
}
}```
You fill each array manually, and then it sorts the array.
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);
}```
In case you need to look at the actual class, here it is.
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;
}
}```
Thanks in advance for any help with this issue!
Last edited by RAW-BERRY; 03-26-2012 at 03:48 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?

3. Member
Join Date
Mar 2012
Posts
2
Rep Power
0

## Re: Issues with sorting algorithm

Yes, sorry. It is a runtime error which occurs immediately after inputing all appropriate data.

#### Posting Permissions

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