PreparedStatement, null?

Discussion in 'Spigot Plugin Development' started by yooniks, Jul 25, 2018.

Thread Status:
Not open for further replies.
  1. hello guys, i have problem since 1 hour and i can't fix that =/
    this is an error:
    Code (Text):

    Caused by: java.lang.NullPointerException
        at org.sqlite.PrepStmt.<init>(PrepStmt.java:42)
        at org.sqlite.Conn.prepareStatement(Conn.java:404)
        at org.sqlite.Conn.prepareStatement(Conn.java:399)
        at org.sqlite.Conn.prepareStatement(Conn.java:383)
        at xyz.yooniks.enderchest.enderchest.EnderchestInitializer.findByPermission(EnderchestInitializer.java:38)
     
    this is my code:
    Code (Text):

    try (final Connection connection = this.database.getConnection();
        final PreparedStatement select = connection.prepareStatement("SELECT * FROM `enderchest_users` WHERE uuid=?")) {
      select.setString(1, player.getUniqueId().toString());
     
    variable 'select' is at line 38, database is not null, connection is not null, i don't know what is =/ that's why i am here
     
  2. The only thing I can see there which can be null is either the statement or the player. Add some debugging for those two
     
  3. I recommend only having the connection in the try statement’s signature, not the PS too. And am I missing something? Why final variables?
     
  4. why not in the preparedstatements? so what i am supposed to having in ps? Why not final variables?
    i will try that
     
  5. The statement needs to be closed, so that's 100% fine and even recommended. Final is also encouraged due to the fact it's not supposed to be changed; Java just makes it too verbose to use it often (e.g. look at Kotlin's var and val, Rust's, Haskell's, Clojure's, etc. immutable-by-default).
     
    • Useful Useful x 1
  6. Hmm yea it made sense to me too, I’ve just never seen anybody do it before. Never seen final in a try-with-resources and do you have to close a prepared statement? If not why does it have to be there?

    Edit: ik about closing prepared statements nvm.
     
  7. Final should be used everywhere it can be, honestly.
    If you couldn't close one, it couldn't've been there.
     
  8. Yup, just being stupid. Good ol’ me. I use final wherever I can too, obviously, but it’s just one of those thins - I’ve never seen one being used there but it makes total sense.
     
  9. Still throws npe =/
    Code (Text):

    public EnderchestHandler findByPermission(Player player) {

      Validate.notNull(player);
      Validate.notNull(this.database);
      Validate.notNull(this.database.getConnection());

      List<ItemStack> content = Lists.newArrayList(player.getEnderChest().getContents());

      try (final Connection connection = this.database.getConnection();
          final PreparedStatement select = connection.prepareStatement("SELECT * FROM `enderchest_users` WHERE uuid=?")) {

        select.setString(1, player.getUniqueId().toString());
        final ResultSet result = select.executeQuery();
        if (result.next()) {
          player.sendMessage("result - next");
          content = Arrays.asList(
              SerializerHelper.deserializeInventory(
                  result.getString("enderchest_content"))
          );
        }
     
     
  10. Try-with-resources are assured to close the resource after the block has been executed. They eliminate the ugliness of the traditional try-catch-finally and clean up after themselves.

    Can you show the error again with the new code?
     
  11. I know how they work, thanks.
    Are you using 1 connection or some pooling?
     
  12. According to some source code I found (not sure if it's up to date or matches OPs dependency, but it had the same package and class (org.sqlite.PrepStmt), so I went with it) the database is closed (only reasonable place it seems to be setting an internal field of Conn, db, to null). Since it's a fresh PrepStmt, I reckon the rs field accessed in its constructor is fine.

    Secondly, since connection checks are done before even reaching the PrepStmt constructor, I'd have to guess that the connection object is used in multiple threads at the same time, which is not allowed (this stuff happens, as you closed the connection). Alternatively, you returned a closed connection to a connection pool.

    Moreover, I'd like to add that concurrent access to an SQLite database is not possible. The most concurrency you get out of SQL is concurrent reads with Write Ahead Logging (where writing to SQLite is still only allowed by a single thread)
     
  13. I removed my code from async task, removed try-with-resources, and still not working =/
    error:
    Code (Text):

    xxx
     
    code:
    Code (Text):

    xxx
     
    line 37 is .prepareStatement (in select)
    saving method is also not working:
    error:
    Code (Text):

    xxx
     
    code:
    Code (Text):

    xxx
    }
     
    the only thing that may cause problem is sqlite.
     
    #13 yooniks, Jul 26, 2018
    Last edited: Jul 26, 2018
  14. Can you give the source for this?
     
  15. it just creates table if does not exist, with statement, it does not throw any exceptions
    there is a problem sqlite, i changed my database to mysql and it's working great. I've never used sqlite, that was my first time, i did not know that sqlite has problems with preparedstatements.
    unsolved, but to close.
     
Thread Status:
Not open for further replies.