l have an error when running the code this is my code and it appears "Exception in thread "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);
}
}
}
The null pointer is myCollection
, which you've initialized but never assigned a value later on. I assume you meant to assign the return value from myDb.getCollection()
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.