Java 驱动程序在 cursor.hasNext() 上抛出空指针异常
_collection = db.getCollection("users");
DBCursor cursor = _collection.find();
System.out.println( "cursor len " + cursor.size() );
//outputs: "cursor len 2"
try {
while(cursor.hasNext()) { //Throws null pointer exception
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
mongo shell:
db.users.find()
{ "_id" : ObjectId("51be2534036412d6b648354b"), "name" : "dude" }
{ "_id" : ObjectId("51be4de19234ec062efe4a53"), "name" : "lewbowski" }
为什么??:(
堆栈跟踪:
Jun 16, 2013 5:28:36 PM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to localhost/127.0.0.1:27017 b/c of error
java.lang.NullPointerException
at org.v2d2.data.UserDAO.put(UserDAO.java:166)
at org.bson.BasicBSONCallback._put(BasicBSONCallback.java:174)
at org.bson.BasicBSONCallback.gotObjectId(BasicBSONCallback.java:143)
at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:160)
at org.bson.BasicBSONDecoder._decode(BasicBSONDecoder.java:79)
at org.bson.BasicBSONDecoder.decode(BasicBSONDecoder.java:57)
at com.mongodb.DefaultDBDecoder.decode(DefaultDBDecoder.java:61)
at com.mongodb.Response.<init>(Response.java:83)
at com.mongodb.DBPort.go(DBPort.java:142)
at com.mongodb.DBPort.call(DBPort.java:92)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
at com.mongodb.DBCursor._check(DBCursor.java:368)
at com.mongodb.DBCursor._hasNext(DBCursor.java:459)
at com.mongodb.DBCursor.hasNext(DBCursor.java:484)
at org.v2d2.data.UserDAO.<init>(UserDAO.java:76)
at MongoTest.main(MongoTest.java:41)
==================================================== =========
我修复了空指针,但我离得更近了。当我尝试从类中获取值时,它们返回 null。
public class UserDAO implements DBObject {
private String _name;
private String _id;
private ArrayList<String> _buildServers;
private BasicDBObject _dao;
private DBCollection _collection;
public UserDAO() { }
public UserDAO(
String name,
DB db )
{
_name = name;
_dao = new BasicDBObject();
_collection = db.getCollection("users");
_dao.put( "name", _name);
DBCursor cursor = _collection.find();
System.out.println( "curso len " + cursor.count() );
try {
while(cursor.hasNext()) {
System.out.println( ((UserDAO)cursor.next()).GetName() );
}
} finally {
cursor.close();
}
}
public void SetName( String value )
{
_name = value;
}
public String GetName()
{
return _name;
}
@Override
public boolean containsField(String arg0)
{
return _dao.containsField( arg0 );
}
@Override
@Deprecated
public boolean containsKey(String arg0)
{
return _dao.containsKey( arg0 );
}
@Override
public Object get(String arg0)
{
return _dao.get( arg0 );
}
@Override
public Set<String> keySet()
{
return _dao.keySet();
}
@Override
public Object put(String arg0, Object arg1)
{
//Error was being thrown in here added null check but I can't set the values
if( _dao == null ) _dao = new BasicDBObject();
return _dao.put( arg0, arg1 );
}
@Override
public void putAll(BSONObject arg0)
{
// TODO Auto-generated method stub
_dao.putAll( arg0 );
}
@Override
public void putAll(Map arg0)
{
// TODO Auto-generated method stub
_dao.putAll( arg0 );
}
@Override
public Object removeField(String arg0)
{
// TODO Auto-generated method stub
return _dao.removeField( arg0 );
}
@Override
public Map toMap()
{
return _dao.toMap();
}
@Override
public boolean isPartialObject()
{
// TODO Auto-generated method stub
return _dao.isPartialObject();
}
@Override
public void markAsPartialObject()
{
// TODO Auto-generated method stub
_dao.markAsPartialObject();
}
}