首页 > 解决方案 > 从数据库中检索数据时出现空指针异常

问题描述

我正在尝试使用 java jdbc 连接从数据库中的相应源系统中检索表名、表中所有行的计数、最大更新时间戳以及相应的源系统。到目前为止,这就是我想出的。

public Map<String,Long> getGpTableCount() throws SQLException {
    List<String> gpTableList        = getChunks();                          // sourcedb.tablenamename,targetdb.tablename:SSN
    Iterator<String> keySetIterator_gpTableList = gpTableList.iterator();   // sourcedb.tablename,targetdb.tablename:SSN
    Connection sourcedbCon      = (Connection) DbManager.getGpConnection();
    PreparedStatement gp_pstmnt     = null;
    String gpExcpnMsg;
    while(keySetIterator_gpTableList.hasNext()) {
        String gpTabSchemakey   = keySetIterator_gpTableList.next();    // sourcedb.tablename,targetdb.tablename:SSN
        String[] tablesnSSNs    = gpTabSchemakey.split(",");            // tablesnSSNs[0]: sourcedb.tablename, tablesnSSNs[1]: targetdb.tablename:SSN
        String[] target         = tablesnSSNs[1].split(":");            // target[0]=targetdb.tablename, target[1]=SSN
        String[] sourcedbTable  = target[0].split("\\.");               // sourcedbTable[0] = targetdb, sourcedbTable[1]=table
        String gpCountQuery      = "select '" + sourcedbTable[1] + "' as TableName, count(*) as Count, source_system_name, max(xx_last_update_tms) from " + tablesnSSNs[0] + " where source_system_name = '" + target[1] + "' group by source_system_name";
        System.out.println("\nGP Query: " + gpCountQuery);
        try {
            gp_pstmnt            = sourcedbCon.prepareStatement(gpCountQuery);
            ResultSet gpCountRs  = gp_pstmnt.executeQuery();        // gpCountRs(1): table, gpCountRs(2): count, gpCountRs(3): source_system_name, gpCountRs(4): max(xx_last_update_tms)
            while(gpCountRs.next()) {
                System.out.println("sourcedbTable[1]: " + sourcedbTable[1] + " gpCountRs.getString(3): " + gpCountRs.getString(3) + " gpCountRs.getLong(2): " + gpCountRs.getLong(2));
                gpCountMap.put(sourcedbTable[1] + "," + gpCountRs.getString(3), gpCountRs.getLong(2));  // (tableName,ssn,12345678)
            }
        } catch(org.postgresql.util.PSQLException e) {
            System.out.println("In SQL Exception block");
            gpExcpnMsg = e.getMessage();
            gpTabNotFound.put(gpCountQuery, gpExcpnMsg + "\n");
        } catch(SQLException e) {
            e.printStackTrace();
        } catch(Exception e) {
            System.out.println("In Exception block");
            gpExcpnMsg = e.getMessage();
            e.printStackTrace();
        }
    }
    System.out.println("GP Connection closed");
    gp_pstmnt.close();
    sourcedbCon.close();
    return gpCountMap;
}

gpTableList 是一个 arrayList,它以 arraylist 的形式接收来自的数据:getChunks() 我在代码旁边的注释中分解了 arralist 中存在的数据格式的方法。这里的问题是当我运行这个方法时,我从表中得到了正确的数据,但它也立即产生了一个空指针异常。形成的查询、结果和异常消息如下所示:

GP Query: select 'xx_table_name' as TableName, count(*) as Count, source_system_name, max(xx_last_update_tms) from sourcedb.xx_table_name where source_system_name = 'ORACLE' group by source_system_name
sourcedbTable[1]: xx_table_name gpCountRs.getString(3): ORACLE gpCountRs.getLong(2): 5181260
In Exception block
Exception Message: null

java.lang.NullPointerException
        at com.recordcount.dao.ChunkData.getGpTableCount(ChunkData.java:117)
        at com.recordcount.entry.StartCount.main(StartCount.java:53)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

异常对应的行在:

gpCountMap.put(analyticsTable[1] + "," + gpCountRs.getString(3), gpCountRs.getLong(2));

这是我的主要课程:

public class StartCount {

    public static void main(String[] args) throws SQLException, IOException, AddressException, InterruptedException {
        ChunkData chData = new ChunkData();
        chData.getGpTableCount();
    }
}

我试图调试代码,但我无法理解我犯的错误。谁能让我知道我该如何解决这个问题?

标签: javajdbc

解决方案


根据您的代码,gpCountMap为空,您已将其设为全局变量,但未对其进行初始化。

为了解决它,只需在开头添加以下代码getGpTableCount

gpCountMap = new HashMap<String,Long>();

或者在main方法中添加:

public static void main(String[] args) throws SQLException, IOException, AddressException, InterruptedException {
    ChunkData chData = new ChunkData();
    chData.getGpTableCount();
    gpCountMap = new HashMap<String,Long>();
}

推荐阅读