[Solved] MySQL connection

Hey guys!

I’m very new to Sponge and I recently started programming in Java, which is why I don’t really know how things exactly work, but I’d like to figure it out.

I’ want to establish a MySQL connection using the Sponge API, but I can’t make it work for me. I’ ve got this:

    ResultSet rs;
    private Game game;
    private SqlService sql;
    public DataSource getDataSource(String jdbcUrl) throws SQLException  {

            if(sql == null)
            {
                sql = game.getServiceManager().provide(SqlService.class).get();
            }

        return sql.getDataSource(jdbcUrl);

    }

And

@Subscribe
public void onSomeEvent(SomeEvent e) throws SQLException
{
      Connection conn = getDataSource("jdbc:mysql://myuser:mypass@localhost:3306/mydatabase").getConnection();

       try
       {
           rs=conn.prepareStatement("SELECT something FROM mytable WHERE something='something'").executeQuery();
       } finally {
           conn.close();
       }
}

The rest of the plugin runs fine, but as soon as this event is triggered I get a NullPointerException… What am I doing wrong? (btw I got this code from the Sponge docs)

Where are you setting your “game” variable?

You will either need to listen to an event and set it, or use injection from your main plugin class.

https://docs.spongepowered.org/en/plugin/basics/injection.html?highlight=game

1 Like

I would imagine the NullPointerException is being thrown from here:

rs=conn.prepareStatement("SELECT something FROM mytable WHERE something='something'").executeQuery();

The reason I say that, is because it’s the first attempt to use the potentially null object which you get from here:

sql = game.getServiceManager().provide(SqlService.class).get();

You need to check that the value is actually present.

Optional<SqlService> sql = game.getServiceManager().provide(SqlService.class)
if (sql.isPresent()) {
    // do sql stuff
}

The reason that Optional#get() is returning null is most likely because it’s not implemented yet, but I’m not certain about that. If I remember correctly, @zml has done the most work on SqlService and SqlServiceImpl so could probably help you more on this.

2 Likes

Thank you for your help @ryantheleach and @FerusGrim, it was indeed this line which caused the NullPointerException. Seems like my brain doesn’t work properly today, i actually forgot to set the game variable (which turned out to be null).

I also did

Optional<SqlService> sql = game.getServiceManager().provide(SqlService.class)
if (sql.isPresent()) {
    // do sql stuff
}

in order to check if the variable is present and it actually is. After another issue which took me another 20 minutes to figure out (I forgot to switch on the MySQL server… xD) I finally got the connection.

Thank you for your help!

If you call Optional#get() on an Optional.absent() it will not return null but throw an IllegalStateException. You were thinking of Optional#orNull() here I think.

1 Like