| Author |
ArrayList Sorting Using Comparator? Exception at Runtime
|
Soumya Ranjan Mohanty
Ranch Hand
Joined: Mar 07, 2010
Posts: 44
|
|
I am getting Exception at Runtime.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Soumya Ranjan Mohanty wrote:
I am getting Exception at Runtime.
Yea. I think you're gonna need to give us a bit more detail than that.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Himanshu Mall
Greenhorn
Joined: Jun 28, 2010
Posts: 22
|
|
1) Please check your ObjComp constructor, the instance member "name" will contain null for all of the objects.
2) Provide a valid implementation of public String toString() if you want to print out the list elements. (Check out Arrays.toString() method as well. <It is really interesting>)
What the program would print now?
[Glen, Michael, Ricky, Shane, Shaun, Stuart]
Hope it helps.
Himanshu Mall
@mall.himanshu84@gmail.com
|
Regards
Himanshu Mall
@mall.himanshu84@gmail.com
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
Check your ObjComp class, and its field variable initializations......
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Soumya Ranjan Mohanty
Ranch Hand
Joined: Mar 07, 2010
Posts: 44
|
|
|
Would you please modify my code and make it working. I want to sort the elements in the ArrayList Using the Collections.sort().
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Soumya Ranjan Mohanty wrote:Would you please modify my code and make it working. I want to sort the elements in the ArrayList Using the Collections.sort().
Himanshu Mall explained it. Have a look on it.
|
 |
Unmesh Chowdhury
Ranch Hand
Joined: Jun 20, 2010
Posts: 44
|
|
You just modify your ObjComp class constructor as follows:
And override your toString() method, for instance:
Otherwise, Object class toString() will be called for your ObjComp class instances.
|
M.Sc. in CS, OCPJP6 93%
|
 |
 |
|
|
subject: ArrayList Sorting Using Comparator? Exception at Runtime
|
|
|