0

I have tried the following code, it displayed the date values for the first two times of the run, but later started throwing a nullpointerexception, any idea why this is happening?

DBCursor c=test.find();
while(c.hasNext())
{
    DBObject dbc=c.next();
    Date n=(Date)dbc.get("time");
    System.out.println(n);

    Calendar cal = new GregorianCalendar();
    cal.setTime(n);
    System.out.println(cal.get(Calendar.DAY_OF_MONTH));
 }

1 Answers1

0

The only line where such an exception can occur is cal.setTime(n), if n is null. This may happen if the documents retrieved from the MongoDB collection read through the cursor lack a time attribute. Check the structure of your documents. But next time post the output of your program.

Nicolas
  • 2,151
  • 1
  • 25
  • 29