0

I'm writing a small web app using a SQlite database to store users information. But I cannot figure out why I get a NullPointerException if the password entered is wrong only once the first time.

Here is my method checkPasssword() :

private boolean checkPassword(String password) throws SqlJetException
{
    boolean valid = false;

    File fileDb = new File(DB_FILE);
    db = SqlJetDb.open(fileDb, true);

    db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
    try
    {
        ISqlJetTable table = db.getTable(USERS_TABLE);
        ISqlJetCursor cursor = table.lookup(PASSWORD_FIELD, sha1(password));
        if (!cursor.eof())
        {
            valid = true;
        }
    } 
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    } 
    finally 
    {
        if (db != null)
        {
            db.commit();
            db.close();
        }
    }

    return valid;
}

At this line :

db.close();

I get a NullPointerException only if the password is wrong the first time even with the if condition before. If the password is correct the first time and wrong a second time it works! It works every time the password is correct.

Here is the StackTrace :

# java.lang.NullPointerException
at org.tmatesoft.sqljet.core.table.engine.SqlJetEngine.runSynchronized(SqlJetEngine.java:219)
at org.tmatesoft.sqljet.core.table.engine.SqlJetEngine.close(SqlJetEngine.java:233)
at Login.checkPassword(Login.java:91)
at Login.doPost(Login.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:738)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:551)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:478)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:462)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:279)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:534)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Thread.java:744)

Any idea ???

Edit : I solved it just by moving db.close() outside of the finally block.

Jongware
  • 22,200
  • 8
  • 54
  • 100
didil
  • 693
  • 8
  • 22

0 Answers0