2015-06-16 41 views
0

我正在学习MongoDB课程MongoDB University。对于作业,我已经完成了所有必要的事情,但是在根据日期的降序对博客文章进行排序时,我得到了NullPointerException。我尝试了很多东西,但无法摆脱它。有谁能够帮助我?根据MongoDB中的日期排序

这是我的Java代码,当我在数据库中插入博文时。

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); 

这里是我使用的代码,基于日期的降序排序。

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; 
} 

这里是我得到的异常堆栈跟踪。

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) 
+0

posts.add(d)是行号57 –

回答

1

你的问题与MongoDB无关。这是一个简单的Java错误。你与你的声明清单:现在

List<Document> posts = null; 

posts是一个空指针,所以当你尝试做posts.add(d);你得到一个NullPointerException。相反初始化posts = null的,它初始化为new ArrayList(或实现List另一个类)

+0

我认为这是最愚蠢的错误,任何人都做过。感谢您指出。 –

+0

@HardikModha你可能会考虑从你的代码中完全禁用'null'关键字。它几乎总是可以避免的。在极少数情况下,null值代表“我不知道”或“不适用”,您可以使用['Optional'](http://www.oracle.com/technetwork/articles/java) /java8-optional-2175753.html)。当它代表“我找不到它”时,不要返回null,抛出异常。 – Philipp

+0

感谢您的信息@Philipp。昨天我在MongoDB内部如此深刻,以至于我甚至找不到这个愚蠢的错误,并浪费了很多时间。 –