繁体   English   中英

Java-Derby-JDBC-NullPointerException PreparedStatement

[英]Java - Derby - JDBC - NullPointerException PreparedStatement

我的程序有问题。

我使用Derby数据库并在其上连接JDBC。 但是,每次调用对数据库具有“某些功能”的函数时,每次得到nullpointerexception。

因此这是一个例外:

java.sql.SQLException: Java exception: ': java.lang.NullPointerException'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.javaException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at notetool.database.Database_Manager.getCatDirs(Database_Manager.java:161)
at Notetool.<init>(Notetool.java:30)
at Notetool.main(Notetool.java:23)
Caused by: java.sql.SQLException: Java exception: ': java.lang.NullPointerException'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 17 more
Caused by: java.lang.NullPointerException
at org.apache.derby.iapi.types.TypeId.getTypeId(Unknown Source)
at org.apache.derby.iapi.types.DataTypeDescriptor.getType(Unknown Source)
at org.apache.derby.impl.sql.catalog.SYSCOLUMNSRowFactory.buildDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getDescriptorViaIndexMinion(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getDescriptorViaIndex(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getColumnDescriptorsScan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getColumnDescriptorsScan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.finishTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getTableDescriptorIndex1Scan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getUncachedTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.NameTDCacheable.setIdentity(Unknown Source)
at org.apache.derby.impl.services.cache.ConcurrentCache.find(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.QueryTreeNode.getTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.FromList.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.SelectNode.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source)
at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 10 more

第161行如下:

PreparedStatement statement = con.prepareStatement("SELECT name FROM dir");

以及完整的功能:

public ArrayList<String> getCatDirs() {
    con = initConnection();
    ArrayList<String> dirs = new ArrayList<String>();
    ResultSet resultSet;

    try {

        PreparedStatement statement = con.prepareStatement("SELECT name FROM dir");
        resultSet = statement.executeQuery();
        int columnCnt = resultSet.getMetaData().getColumnCount();

        while (resultSet.next()) {
            for(int i = 1; i <= columnCnt;i++){
              dirs.add(resultSet.getString(i) +  " ");
            }
          }

        resultSet = statement.executeQuery("SELECT MAX(dirid) FROM dir");
        columnCnt = resultSet.getMetaData().getColumnCount();
        while( resultSet.next()) {
            setCatIds(resultSet.getInt(columnCnt));
        }

        con.close();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return dirs;
}

这也是initConnection()函数

public Connection initConnection() {
    try {

        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        con = DriverManager.getConnection("jdbc:derby:db/notedb;create=true;user=admin;password=1234;schema=admin");

    } catch (SQLException | ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    return con;
}

我真的不知道出什么问题了。 大约一个小时前,它运行良好。

也许有帮助,这是derby.log的输出

------------  BEGIN SHUTDOWN ERROR STACK -------------

ERROR XSDB0: Unexpected exception on in-memory page Page(5,Container(0, 144))
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.store.raw.data.StoredPage.initSlotTable(Unknown Source)
at org.apache.derby.impl.store.raw.data.StoredPage.initFromData(Unknown Source)
at org.apache.derby.impl.store.raw.data.CachedPage.setIdentity(Unknown Source)
at org.apache.derby.impl.services.cache.ConcurrentCache.find(Unknown Source)
at org.apache.derby.impl.store.raw.data.FileContainer.getUserPage(Unknown Source)
at org.apache.derby.impl.store.raw.data.FileContainer.getPage(Unknown Source)
at org.apache.derby.impl.store.raw.data.BaseContainerHandle.getPage(Unknown Source)
at org.apache.derby.impl.store.access.conglomerate.OpenConglomerate.latchPage(Unknown Source)
at org.apache.derby.impl.store.access.conglomerate.GenericConglomerateController.fetch(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getDescriptorViaIndexMinion(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getDescriptorViaIndex(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getColumnDescriptorsScan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getColumnDescriptorsScan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.finishTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getTableDescriptorIndex1Scan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getUncachedTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.NameTDCacheable.setIdentity(Unknown Source)
at org.apache.derby.impl.services.cache.ConcurrentCache.find(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.QueryTreeNode.getTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.FromList.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.SelectNode.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source)
at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at notetool.database.Database_Manager.getCatDirs(Database_Manager.java:161)
at Notetool.<init>(Notetool.java:30)
at Notetool.main(Notetool.java:23)


------------  END SHUTDOWN ERROR STACK -------------

Fri Jan 03 23:19:42 CET 2014 Thread[main,5,main] (XID = 1389), (SESSIONID = 1),        (DATABASE = db/notedb), (DRDAID = null), Cleanup action starting
Fri Jan 03 23:19:42 CET 2014 Thread[main,5,main] (XID = 1389), (SESSIONID = 1), (DATABASE = db/notedb), (DRDAID = null), Failed Statement is: SELECT name FROM dir
java.lang.NullPointerException
at org.apache.derby.iapi.types.TypeId.getTypeId(Unknown Source)
at org.apache.derby.iapi.types.DataTypeDescriptor.getType(Unknown Source)
at org.apache.derby.impl.sql.catalog.SYSCOLUMNSRowFactory.buildDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getDescriptorViaIndexMinion(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getDescriptorViaIndex(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getColumnDescriptorsScan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getColumnDescriptorsScan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.finishTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getTableDescriptorIndex1Scan(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getUncachedTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.catalog.NameTDCacheable.setIdentity(Unknown Source)
at org.apache.derby.impl.services.cache.ConcurrentCache.find(Unknown Source)
at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.QueryTreeNode.getTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.FromList.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.SelectNode.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source)
at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at notetool.database.Database_Manager.getCatDirs(Database_Manager.java:161)
at Notetool.<init>(Notetool.java:30)
at Notetool.main(Notetool.java:23)
Cleanup action completed

如前所述,大约两个小时前,它运行良好。 我向数据库中添加了一个带有SquirrelSQL的表,该表可以正常工作,但是突然崩溃了,我不知道如何使其重新工作。

感谢您的帮助!

不幸的是,您的数据库似乎已损坏。

您是否有可以还原的数据库备份?

这不应该发生,并且可能表明Derby中存在错误。

在尝试重现该问题之前,应保存数据库文件夹和derby.log以及您可以收集的任何其他信息,并与derby-dev邮件列表上的开发人员联系; 也许他们可以帮助您弄清楚数据库是如何损坏的。

包括有关您正在使用的Derby版本,Java的版本,运行的操作系统等的所有信息。

您可以使用新的空数据库来重现此问题吗?

使用Squirrel添加表时,您的程序是否也在同时运行?

在数据库损坏之前是否还有其他奇怪的事件发生? (您的程序崩溃了,您的计算机崩溃了,您的磁盘已满,程序耗尽了内存,等等?)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM