2015-06-16 46 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內部如此深刻,以至於我甚至找不到這個愚蠢的錯誤,並浪費了很多時間。 –