0

I am taking course on MongoDB at MongoDB University. For the homework I have done all the things required but while sorting the blog post based on descending order of date I am getting NullPointerException. I tried many things but can't get rid of it. Can anybody help me?

Here is my Java Code when I insert the blog post in the database.

Document post = new Document();

    post.append("title", title);
    post.append("author", username);    
    post.append("body", body);
    post.append("permalink", permalink);
    post.append("tags", new BasicDBList());
    post.put("tags", tags);
    post.append("comments", new BasicDBList());

    post.append("date", new BsonDateTime(System.currentTimeMillis()));
    postsCollection.insertOne(post);

Here is the code that I am using to sort based on descending order of date.

public List<Document> findByDateDescending(int limit) {

    // XXX HW 3.2,  Work Here
    // Return a list of DBObjects, each one a post from the posts collection

    List<Document> posts = null;
    FindIterable<Document> cursor = postsCollection.find().sort(new BasicDBObject("date", -1));

    Iterator itrtr = cursor.iterator();
    while(itrtr.hasNext())
    {
        Document d = (Document)itrtr.next();
        System.out.println(d);
        posts.add(d);
    }
    return posts;
}

and here is the exception stack trace that I am getting.

Document{{_id=55802877234b082104416d86, title=hello, author=hardik, body=hello world post, permalink=hello, tags=[hello, world, me], comments=[], date=Tue Jun 16 19:15:27 IST 2015}}    Document{{_id=55802877234b082104416d86, title=hello, author=hardik, body=hello world post, permalink=hello, tags=[hello, world, me], comments=[], date=Tue Jun 16 19:15:27 IST 2015}}   java.lang.NullPointerException
at course.BlogPostDAO.findByDateDescending(BlogPostDAO.java:57)
at course.BlogController$1.doHandle(BlogController.java:117)
at course.BlogController$FreemarkerBasedRoute.handle(BlogController.java:97)
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:139)
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:54)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:179)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:451)
at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:266)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
at java.lang.Thread.run(Thread.java:744)
Hardik Modha
  • 12,098
  • 3
  • 36
  • 40

1 Answers1

1

Your problem has nothing to do with MongoDB. It is a simple Java mistake. You declare your list with:

List<Document> posts = null;

posts is now a null-pointer, so when you try to do posts.add(d); you get a NullPointerException. Instead of initializing posts = null, initialize it to a new ArrayList (or another class which implements List)

List<Document> posts = new ArrayList<Document>();
Philipp
  • 67,764
  • 9
  • 118
  • 153
  • I think that is the silliest mistake anyone have ever made. Thanks for pointing out. – Hardik Modha Jun 16 '15 at 13:55
  • 1
    @HardikModha You might consider banning the `null` keyword from your code altogether. It can almost always be avoided. In the rare situation where a value of `null` stands for "I don't know" or "not applicable", you can use [`Optional`](http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html). When it stands for "I couldn't find it", don't return null, throw an exception. – Philipp Jun 16 '15 at 14:06