运行代码时出错 这是我的代码,它出现在"线程中的异常" main" java.lang.NullPointerException"
import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class test_3{
public static void main(String[] args) {
try {
DB myDb ;
DBCollection myCollection = null;
Mongo mongo = new Mongo("localhost",27017);
myDb=mongo.getDB("test_company");
myDb.getCollection("login");
System.out.println((myCollection.find()).toString());
} catch (UnknownHostException ex) {
Logger.getLogger(test_3.class.getName()).log(Level.SEVERE,
null,ex);
}
}
}
答案 0 :(得分:0)
空指针是myCollection
,您已初始化但之后从未分配过值。我假设您打算从myDb.getCollection()
分配返回值?