简体   繁体   English

MongoDB Java-API NoSuchElement

[英]MongoDB Java-API NoSuchElement

I'm builing on a Java API for MongoDB. 我在建立用于MongoDB的Java API。 More specific, right now I'm trying to encapsulate the process of connecting to MongoDB and choosing a database and a collection from that database to it's own class in a single method. 更具体地说,现在,我正在尝试用单一方法封装连接到MongoDB并选择数据库和该数据库中的集合到其自己的类的过程。

package mongoDB;

import java.net.UnknownHostException;
import java.util.*;
import com.mongodb.*;

public class MongoDBConnector {
 public DBCollection mongoEasy (String hostname, int port) {

    try {

        Mongo m = new Mongo(hostname, port);

        //choose database
        List<String> databaseNames = m.getDatabaseNames();
        System.out.println("Choose database:  " + databaseNames.toString());
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        if(!databaseNames.contains(input.toString())){
            System.out.println("Database does not exist.");
            System.exit(1);
        }
        DB db = m.getDB(input.toString());

        //choose collection
        Set<String> collectionNames = db.getCollectionNames();
        System.out.println("Choose collection " + collectionNames.toString());
        input = scanner.nextLine();
        if(!collectionNames.contains(input.toString())){
            System.out.println("collection does not exist");
            System.exit(1);
        }
        DBCollection dbCollection = db.getCollection(input.toString());
        scanner.close();
        return dbCollection;

        } catch (UnknownHostException e) {
            System.out.println("Connection failed.");
            System.exit(1);
        } catch(MongoException e){
            System.out.println("Fail.");
            System.exit(1);
        } finally{
        }
    return null;

 }
}

If i create an instance of MongoDBConnector and call the mongoEasy a single time within a main method everything is fine. 如果我创建MongoDBConnector的实例并在main方法中一次调用mongoEasy,一切都很好。 However if i call the method a second time it pumps out a NoSuchElementException. 但是,如果我第二次调用该方法,它将抽出NoSuchElementException。 Take this example: package mongoDB; 举个例子:package mongoDB;

import com.mongodb.*;
import mongoDB.MongoDBConnector;

public class MongoDBOperations2 {
 public static void main(String[] args) {

    MongoDBConnector mongoDBConnector = new MongoDBConnector();
    DBCollection collection = mongoDBConnector.mongoEasy("localhost", 27017);

    collection = mongoDBConnector.mongoEasy("localhost", 27017);
 }
}

Any time I'm trying to execute this I get (I deleted the database and collection names and line number for main method doesn't fit because i simplefied it for the question): 每当我尝试执行此操作时,我都会得到(我删除了数据库,并且main方法的集合名称和行号不合适,因为我为问题简化了它):

Choose database [...]
test
Choose collection [...]
test
Choose database [local, StudiumUlm, test]
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at mongoDB.MongoDBConnector.mongoEasy(MongoDBConnector.java:18)
    at mongoDB.MongoDBOperations2.main(MongoDBOperations2.java:)

The second time I call the method the scanner is not waiting for my input. 第二次调用该方法时,扫描仪不等待我的输入。 Any suggestions? 有什么建议么?

Thanks. 谢谢。 Cheers, Magnus 干杯,马格努斯

According to the Scanner.close() documentation, it will close the underlying Reader/Stream if it implements Closeable . 根据Scanner.close()文档,如果实现了Closeable ,它将关闭底层的Reader / Stream。

Since your code closes the Scanner, you can't read from System.in after the first time(which is exactly what you are seeing). 由于您的代码关闭了扫描程序,因此您在第一次之后就无法从System.in中读取(这正是您所看到的)。

Why not add a close method to your MongoDBConnector like : 为什么不向您的MongoDBConnector添加close方法,例如:

public class MongoDBConnector {

    Scanner scanner;
    public MongoDBConnector() {
       this.scanner = new Scanner(System.in);
    }
    public void close(){
        this.scanner.close();
    }
    // the rest of your code but don't create a new scanner or close it 
}

Then in your mainline do : 然后在您的主线中执行:

public class MongoDBOperations2 {
    public static void main(String[] args) {

        MongoDBConnector mongoDBConnector = new MongoDBConnector();
        try {
            DBCollection collection = mongoDBConnector.mongoEasy("localhost", 27017);

            collection = mongoDBConnector.mongoEasy("localhost", 27017);
        }finally {
            mongoDBConnector.close();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM