This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                2年前关闭。
            
                    
启动我的应用程序时出现以下错误:

java.lang.NullPointerException
        at me.MyApp.MyApp.Database.getConn(Database.java:30) ~[?:?]
        at me.MyApp.MyApp.Models.MyAppModel.<init>(MyAppModel.java:18) ~[?:?]


这是我设置数据库类的方法:

public class Database {
    private static MyApp instance = MyApp.getInstance();
    private static Config config = new Config();


private static HikariDataSource ds = new HikariDataSource();

static {

    HikariConfig dbConfig = new HikariConfig();
    dbConfig.setJdbcUrl("jdbc:mysql://localhost:3306/" + config.get("database.database"));
    dbConfig.setUsername(config.get("database.username"));
    dbConfig.setPassword(config.get("database.password"));
    dbConfig.setDriverClassName("com.mysql.jdbc.Driver");
    dbConfig.addDataSourceProperty("cachePrepStmts", "true");
    dbConfig.addDataSourceProperty("prepStmtCacheSize", "250");
        dbConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

}

    public static Connection getConn() {
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}


这是触发错误的模型类:

public class MyAppModel {

    private MyApp instance = MyApp.getInstance();
    private Connection connection;


    public MyAppModel() {
        connection = Database.getConn();
    }

    public void createTable() {
        BukkitRunnable r = new BukkitRunnable() {
            @Override
            public void run() {
                try {
                    String sql = "CREATE TABLE IF NOT EXISTS `myapp` ( " +
                            " `id` INT NOT NULL AUTO_INCREMENT ," +
                            "`uuid` VARCHAR(255) NOT NULL ," +
                            " `join_message` VARCHAR(255) NOT NULL ," +
                            " `quit_message` VARCHAR(255) NOT NULL ," +
                            " `change_points` INT NOT NULL," +
                            " `last_modified` TIMESTAMP NOT NULL," +
                            " PRIMARY KEY (`id`)" +
                            ")";
                    Statement statement = connection.createStatement();
                    statement.executeUpdate(sql);
                } catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        };

        r.runTaskAsynchronously(instance);
    }
}


我认为MyAppModel连接返回null,并且我正在处理TryCatch处理错误,但是我不完全确定为什么它返回null。我究竟做错了什么?

我了解NullPointerException的概念,但不理解为什么我的应用程序会给我这个错误。

最佳答案

在您的getConn()函数中,您调用ds.getConnection(),但从未在您的类中创建变量ds。确保初始化名为ds的HikariDataSource对象。

抱歉,我对正在使用的对象感到困惑,因此将dbConfig更改回原来的状态,然后根据javadocs-> HikariDataSource(HikariConfig配置)使用其构造函数创建新的HikariDataSource对象

因此,您将需要切换对象的初始化,并在HikariDataSource对象的构造函数中使用dbConfig对象。

private static HikariConfig dbConfig;
static { //keep this the same
    dbConfig = new HikariConfig();
    dbConfig.setJdbcUrl("jdbc:mysql://localhost:3306/" + config.get("database.database"));
    dbConfig.setUsername(config.get("database.username"));
    dbConfig.setPassword(config.get("database.password"));
    dbConfig.setDriverClassName("com.mysql.jdbc.Driver");
    dbConfig.addDataSourceProperty("cachePrepStmts", "true");
    dbConfig.addDataSourceProperty("prepStmtCacheSize", "250");
    dbConfig.addDataSourceProperty( "prepStmtCacheSqlLimit", "2048");
}

private static HikariDataSource ds = new HikariDataSource(dbConfig); //intialize here with your newly created HikariConfig object

08-18 01:18