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.

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, 01: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, 04: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.

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:

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, 05: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, 05: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, 06: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.