1

I get the following error when starting my application:

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

Here is how I've set up my Database class:

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;
    }

}

And here is my model class that triggers the error:

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);
    }
}

I think MyAppModel connection returns null and I'm handling the TryCatch handling wrong, but I'm not entirely sure why it's returning null. What am I doing wrong?

I understand the concept of a NullPointerException but not why my application is giving me this error.

kinx
  • 463
  • 5
  • 12
  • 31

1 Answers1

0

In your getConn() function you call ds.getConnection() but the variable ds has never been created within your class. Make sure you initialize the HikariDataSource object called ds.

Sorry I was confused by the objects being used so change the dbConfig back to what you had and just create the new HikariDataSource object using its constructor according to the javadocs -> HikariDataSource(HikariConfig configuration)

Therefore you will need to switch the initialization of the objects and use your dbConfig object within the constructor of your HikariDataSource object.

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
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • Thanks, when changing to this, return ds.getConnection(); breaks? – kinx Jun 08 '17 at 16:24
  • i changed the code, i completely messed up on the objects you were using. you just need to intialize the ds object using its constructor @kinx – RAZ_Muh_Taz Jun 08 '17 at 16:32
  • Thanks, I've changed the code to your suggestion but I still get the same error. Edited question with the update. – kinx Jun 08 '17 at 16:35
  • you just need to create the config object first then use that object in your constructor of your HikariDataSource @kinx – RAZ_Muh_Taz Jun 08 '17 at 16:41
  • http://www.atetric.com/atetric/javadoc/com.zaxxer/HikariCP/2.4.5/com/zaxxer/hikari/HikariDataSource.html is the javadocs i used – RAZ_Muh_Taz Jun 08 '17 at 16:42
  • 1
    im glad it worked! good luck with the rest of your project @kinx – RAZ_Muh_Taz Jun 08 '17 at 16:51