[JAVA-1531] Comment on: "mongodb-ecosystem/tutorial/use-java-dbobject-to-perform-saves.txt" Created: 29/Oct/14  Updated: 08/Oct/15  Resolved: 23/Jun/15

Status: Closed
Project: Java Driver
Component/s: Documentation
Affects Version/s: None
Fix Version/s: None

Type: Improvement
Reporter: Docs Collector User (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 Enterprise, SP1, 64 bits
MongoDB 2.6.5
Mongo java driver 2.12.4
Java 1.7.0_67

Location: http://docs.mongodb.org/ecosystem/tutorial/use-java-dbobject-to-perform-saves/#java-dbobject-to-perform-saves
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20100101 Firefox/17.0
Referrer: http://docs.mongodb.org/ecosystem/drivers/java/
Screen Resolution: 1680 x 1050
repo: docs-ecosystem
source: tutorial/use-java-dbobject-to-perform-saves



 Description   

Hi.

When I implement Java DBObject to Perform Saves' exemple exactly as described in the tutorial, I get the following exception :

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NullPointerException
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1801)
at com.mongodb.DBCollection._checkObject(DBCollection.java:1789)
at com.mongodb.DBCollectionImpl.applyRulesForInsert(DBCollectionImpl.java:392)
at com.mongodb.DBCollectionImpl.insertWithCommandProtocol(DBCollectionImpl.java:381)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:186)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165)
at com.mongodb.DBCollection.insert(DBCollection.java:93)
at com.mongodb.DBCollection.insert(DBCollection.java:78)
at com.mongodb.DBCollection.insert(DBCollection.java:120)
at tweet.Tweet.main(Tweet.java:27)
... 5 more

Could you tell if its a known bug or clarify how to persist a Java object in MongoDB?

Best regards,

Lionel Ketterer

Reporter: Lionel Ketterer
E-mail: lionel.ketterer@thalesgroup.com



 Comments   
Comment by Jeffrey Yemin [ 23/Jun/15 ]

That page has been removed from the documentation entirely, so closing this issue.

Comment by Jeffrey Yemin [ 05/Nov/14 ]

That's a poorly written example. In general, if you want to use POJOs it's better to use a wrapper like Morphia or Spring Data MongoDB. But if you just want to get that example working, try implementing the Tweet class like this:

public class Tweet extends ReflectionDBObject {
    private String userId;
    private Date date;
 
    public Tweet() {
    }
 
    public Tweet(final String userId, final Date date) {
        this.userId = userId;
        this.date = date;
    }
 
    public String getUserId() {
        return userId;
    }
 
    public void setUserId(final String userId) {
        this.userId = userId;
    }
 
    public Date getDate() {
        return date;
    }
 
    public void setDate(final Date date) {
        this.date = date;
    }
 
    @Override
    public String toString() {
        return "Tweet{"
               + "userId='" + userId + '\''
               + ", date=" + date
               + '}';
    }
}

Generated at Thu Feb 08 08:54:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.