Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems while sorting Hashmap based on values.

 
Somesh Rathi
Ranch Hand
Posts: 31
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Arrays.sort(troubleReportInfo);



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

Many thanks in advance !
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the stack trace, please?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic