什么是NullPointerException?

时间:2015-03-13 09:51:56

标签: java nullpointerexception jetty sqljet

我正在使用SQlite数据库编写一个小型Web应用程序来存储用户信息。但是,如果第一次输入的密码错误,我无法弄清楚为什么我会得NullPointerException

以下是我的方法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;
}

在这一行:

db.close();

只有在密码错误的情况下,即使之前有NullPointerException条件,我才会收到if。如果密码是正确的第一次和错误第二次它的工作!每次密码正确时都可以使用。

这是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)

任何想法???

修改:我只是通过将db.close()移到finally块之外来解决它。

0 个答案:

没有答案