File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Problems while sorting Hashmap based on values. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problems while sorting Hashmap based on values." Watch "Problems while sorting Hashmap based on values." New topic

Problems while sorting Hashmap based on values.

Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 31

I am trying to figure out the problem while sorting the hashmap by one of the attributes of the objects stored in it.
For example, the objects stored in the hashmap are of the class 'TroubleReportInfo'. TroubleReportInfo Object consist of Date as Attribute. I want to print the TroubleReportInfo object in descdending order of dates.
I'm getting the NullpointerException while sorting map.

I'm doing the following steps:
1.Using Simple HasMap to Store the TroubleReportInfo Object
2.My TroubleReportInfo Class is implementing the Comparable interface
3. compareTo is written as follows

public int compareTo(Object o)
int i =0;
Date createdDate = null;
Date todayDate = new Date();
if(o instanceof TroubleReportInfo)
createdDate = ((TroubleReportInfo)o).createdDate;
if(createdDate ! = null &&(createdDate.after(todayDate) || createdDate.equals(todayDate)))
i = 1;
return i;
4. I'm using the arrys class sort method

Please help me out at your earliest why it is giving the null pointer Excetion ..

Many thanks in advance !
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Can you post the stack trace, please?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

A Map is an unsorted collection - you cannot sort a Map. From your description (you're using Arrays.sort(...)) it sounds as if you're not really sorting the Map, but an array of TroubleReportInfo objects. Can you explain what you really mean with "sorting the hashmap"?

The stacktrace of the NullPointerException tells you exactly in which line the exception happens. Look at that line in your source code and find out what is null there while it isn't supposed to be.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Well, a Map can be sorted - see the SortedMap and NavigableMap interfaces, commonly implemented by TreeMap. Here the sorting as done as you put entries into the map, not afterwards. Also, the sorting is done based on the keys to the map, not the values. From Somesh's description it's not clear whether we're talking about keys or values, or if we're even really talking about a Map at all. Somesh, can you show some code that shows how these objects are stored or retrieved from the Map? Does the map have keys and values? Which are TroubleReportInfo - keys or values?

Also, I don't think it will make any sense to compare all dates to today's date. You will probably want to compare them to the dates of other TroubleReportInfo objects. Assuming sorting makes sense at all, which is not really clear yet.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Problems while sorting Hashmap based on values.
It's not a secret anymore!