Results 1 to 3 of 3
  1. #1
    RAW-BERRY is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default 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!

  3. #3
    RAW-BERRY is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Issues with sorting algorithm

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

Similar Threads

  1. Replies: 5
    Last Post: 07-11-2011, 01:40 PM
  2. Help with a sorting algorithm
    By doobybug in forum New To Java
    Replies: 2
    Last Post: 01-14-2011, 12:39 PM
  3. Algorithm help? :)
    By Mirix in forum New To Java
    Replies: 6
    Last Post: 05-24-2010, 02:08 AM
  4. Need some help in an algorithm
    By ea09530 in forum New To Java
    Replies: 3
    Last Post: 04-04-2010, 01:13 PM
  5. O(log n) algorithm help !!!!!!
    By itseeker87 in forum New To Java
    Replies: 8
    Last Post: 09-09-2008, 05:12 PM

Posting Permissions

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