gpt4 book ai didi

java - 从数据库检索数据时出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 06:07:34 25 4
gpt4 key购买 nike

我正在尝试使用 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();
}
}

我尝试调试代码,但我无法理解我所犯的错误。谁能告诉我如何解决这个问题?

最佳答案

根据您的代码,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>();
}

关于java - 从数据库检索数据时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50981897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com