java - 获取 JSON 作为输入时出现 MongoDB NULL 指针异常

标签 java mongodb database

package com.demo.mongodb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;

public class Driver {
    private static DBCollection channelDBcollection;

    public static void main(String[] args) throws IOException {
        DB db = (new MongoClient("localhost",27017)).getDB("demo");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        boolean flag = false;
        while(!flag) flag = autenticate(db, bufferedReader);
    }
    private static boolean autenticate(DB db, BufferedReader bufferedReader) throws IOException{
        boolean flag = true;
        System.out.println("User: ");
        String user = bufferedReader.readLine();

        System.out.println("Password: ");
        String password = bufferedReader.readLine();

        if(db.authenticate(user, password.toCharArray())){
            DBCollection channDbCollection = db.getCollection("Channel");
            String command = null;
            while(true){
                System.out.println("What do you want to do ? ");
                command = bufferedReader.readLine();
                if(command.equals("exit")) break ;
                else if(command.equals("findALL")) findAll(channDbCollection);
                else if(command.equals("insertJSON")) insertJSON(bufferedReader,channDbCollection);
            }
        }
        else{
            System.out.println("invalid user/password");
            flag = false;
        }


        return flag;
    }

    private static void findAll(DBCollection dbCollection){
        DBCursor dbCursor = channelDBcollection.find();
        while(dbCursor.hasNext()) System.out.println(dbCursor.next());
    }

    private static void insertJSON(BufferedReader bufferedReader,DBCollection channDbCollection) throws IOException {

        System.out.println("JSON: ");
        channDbCollection.insert((DBObject) JSON.parse(bufferedReader.readLine()));
    }
}

我在 MongoDB 中创建数据库,如下所示:-

use demo

db.addUser("root","root")

当我执行应用程序时,当我输入 JSON 时,我发现空指针有人可以帮助我从中取出吗?

最佳答案

在你的

private static void findAll(DBCollection dbCollection){
    DBCursor dbCursor = channelDBcollection.find();
    while(dbCursor.hasNext()) System.out.println(dbCursor.next());
}

函数中,您使用全局变量channelDBcollection(在调用时为null)而不是参数dbCollection(在方法中的任何地方都没有使用); 您认为“not null”的是具有令人困惑的名称“channDbCollection”的局部变量,但它没有在任何地方使用;

我建议您修改命名方法、使用全局变量和一般编码风格。

关于java - 获取 JSON 作为输入时出现 MongoDB NULL 指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22247253/

相关文章:

mongodb聚合获取当前日期

java - 您如何向六年级学生解释 Scala 的抽象类特性?

java - 对于 Postgres 9.5 上的 PL/Java,mvn clean install 失败

ruby-on-rails - 如何从父模型类调用 before_save 方法?

javascript - 从javascript访问mongodb+nodjs?

php - 获取某个日期范围内的所有表格

mysql - 有条件地创建索引?

stored-procedures - 为什么(以及何时)使用存储过程?

java - 返回任意类型的对象

java - 无法获取 POST 请求正文