File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArrayList Sorting Using Comparator? Exception at Runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ArrayList Sorting Using Comparator? Exception at Runtime" Watch "ArrayList Sorting Using Comparator? Exception at Runtime" New topic
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: 18101
    
  39

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: 45
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%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList Sorting Using Comparator? Exception at Runtime
 
Similar Threads
Generics
problem with cast
JFrame confusion
Generics adding to List (? extends Object)
Sorting Collection