1

I would like to set dynamic Date values to the Map<String,Object> and display that in the UI. I am working on JSF2. Here is my code

private  Map<String, Object> selectDates;
    {
        selectDates = new LinkedHashMap<String, Object>();
        selectDates.put("First Date", "111"); //label, value
        selectDates.put("Second Date", "222");
    }
    public Map<String, Object> getSelectDates()
        {
            return selectDates;
        }

I am having a drop down with label "First Date" and "Second Date" , now i have to assign values to these key dynamically. It does not work if i give as below:

 private  Date exDate;
private Date frontDate;
private  Map<String, Object> selectDates;
    {
        selectDates = new LinkedHashMap<String, Object>();
        selectDates.put("First Date", exDate;); //label, value
        selectDates.put("Second Date", frontDate;);
    }

public Map<String, Object> getSelectDates() {
    return selectDates;
}

I tried using private Map<String, Date> selectDates; but this does not give me value, it gives me null.

3
  • 1
    Make sure exDate and frontDate are initialized before putting them into the Map. Commented Apr 6, 2015 at 15:38
  • Thanks for the response . If i initialize them , i get value as current date but i dont want the current date , exDate and frontDate are the date values from DB . Commented Apr 6, 2015 at 16:22
  • And I mean, initialize them (from whatever data source you have to do it), and after that, put the dates in the Map. You probably should do this in a @PostConstruct method in your bean rather than in an initialize block. Commented Apr 6, 2015 at 16:24

1 Answer 1

1

You are using a non - static initalization code block. This code will only be executed when an instance of the containing class is created. Here is what I've got when I am trying to use your code

public class DatesInHash {

     private  Date exDate;
     private Date frontDate;
     private  Map<String, Object> selectDates;
     {
         //exDate = new Date();
         selectDates = new LinkedHashMap<String, Object>();
         selectDates.put("First Date", exDate); //label, value
         selectDates.put("Second Date", frontDate);
     }

     public Map<String, Object> getSelectDates() {
         return selectDates;
     }

     public void doDemo() {
         exDate = new Date();
         frontDate = new Date();
         Map<String, Object> datesMap = getSelectDates();

         System.out.println(((Date)datesMap.get("First Date")).toString());

     }

     public static void main(String[] args) {
         DatesInHash dIH = new DatesInHash();
         dIH.doDemo();

     }

}

When I execute this application I get a null pointer exception because at the moment the object gets constructed the non - static block gets invoked and since your two Date variables are null, it triggers the exception.

However if I uncomment this line of code //exDate = new Date();, I will work fine and displays a formatted version of the date variable.

Unless you need to use a non - static iniatialization bloc for a very specific reason I suggest that you create a simple initialization method of your map such as

 public void initMap() {

     selectDates = new LinkedHashMap<String, Object>();
     selectDates.put("First Date", exDate); //label, value
     selectDates.put("Second Date", frontDate);


 }

And it will only be called after you are sure that the two dates are well initialized. If using the non - static block is mandatory then you need to properly initialize your date objects first.

More usefull infos on the use of non static initialization code blocks here and here

Sign up to request clarification or add additional context in comments.

2 Comments

@aradhya, please accept my answer if it worked for you
Hi at the left of my answer you have an up arrow to vote up, down arrow to vote down AND 'between' how to accept

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.