0

I'm unit testing a method that uses a Date and everyway I initialize it seems to cause a NPE when I pass it. The HashMap is initialized and I can see the value when I debug to know there is a date but at my put statement the Date just seems to through the exception.

EDIT

Map<Double, Date> actualTimes = new HashMap<>();
Date date = new Date();

testMethod() {  
    clearProductMaps();
    calendar.set(Calendar.MONTH, Calendar.JULY);
    calendar.set(Calendar.DAY_OF_MONTH, 21);
    calendar.set(Calendar.YEAR, 2016);
    date = calendar.getTime();

    try {
        date = sdf.parse("2016-10-15");
    } catch(ParseException ex) {
        System.out.println("ParseException " + ex);
    }

    System.out.println("Debug:" + " " + sdf.format(date).toString());
    if(date != null) {
       actualTimes.put(1.50, date);
    }
    //assertTest call
}

I don't need the date formatted to be apart of the map and I have gotten strange responses with my null conditional check so I'm not what I'm doing wrong.

April_Nara
  • 1,024
  • 2
  • 15
  • 39
  • 1
    `actualTimes.put(1.50, date);` does not compile if `actualTimes` is a `Map`, because `1.50` is not a `String`. – Jesper Aug 22 '16 at 14:14
  • 1
    @Jesper Double is showing in the edit. – April_Nara Aug 22 '16 at 14:19
  • 2
    either change your Map to `Map actualTimes = new HashMap<>();` or either change the attribute, `actualTimes.put("1.50", date);` though i think the first will you more because if you do the second one then you will have trouble in traversing the object in order to use it – Sir. Hedgehog Aug 22 '16 at 14:20
  • 1
    @hedgehog A `Map` is not possible, primitive types such as `int` cannot be used as type arguments in Java. That should be `Map`. – Jesper Aug 22 '16 at 14:31
  • 1
    @Jesper lol man, you got the point of what I meant.... misstype for typing fast..... – Sir. Hedgehog Aug 22 '16 at 14:34
  • @Jesper placed a debug statement to see the value of both my double and date and getting values. Where is this error from with values to these map types. – April_Nara Aug 22 '16 at 14:42
  • @hedgehog placed a debug statement to see the value of both my double and date and getting values. Where is this error from with values to these map types. – April_Nara Aug 22 '16 at 14:42
  • Do you understand why a `NullPointerException` occurs (see the question that this is marked a duplicate of)? Where exactly does the exception happen? What is `null` there? You get a `NullPointerException` if you try to call a method on a variable that is `null`. – Jesper Aug 22 '16 at 14:48
  • @Jesper the nullpointer is occuring because the HashMap initialization sees a null create date variable. It's the line adding the Double and the Date to the Map. – April_Nara Aug 22 '16 at 14:59

0 Answers0