Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Issues with sorting algorithm

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 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:

        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:

    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!


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Location
    Missouri, United States
    Posts
    17
    Thanks
    4
    Thanked 2 Times in 2 Posts

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

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

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

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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:
    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:
    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:
    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.
    Last edited by RAWBERRY; May 1st, 2012 at 04:28 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Issues with sorting algorithm

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,548
    Thanks
    24
    Thanked 299 Times in 282 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with sorting algorithm

    Quote Originally Posted by Norm View Post
    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 View Post
    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.

Similar Threads

  1. Jar Issues
    By DMinton in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2012, 02:32 PM
  2. [SOLVED] Sorting issues.
    By Saintroi in forum What's Wrong With My Code?
    Replies: 16
    Last Post: May 2nd, 2012, 08:53 PM
  3. Basic Java Sorting Algorithm (Selection/Insertion) help
    By Arte7 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 22nd, 2011, 01:38 PM
  4. CRC algorithm
    By aldykid in forum Algorithms & Recursion
    Replies: 1
    Last Post: August 7th, 2011, 10:50 AM
  5. 2 Java methods and a sorting algorithm
    By Tiberius in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 26th, 2011, 02:41 PM