从表收集数据时无法修复java.lang.NullPointerException

时间:2018-06-27 09:00:16

标签: java jdbc

我正在尝试同时从两个数据库中检索数据以进行数据比较。为了获得两个数据库中存在的表名,我必须阅读第三个数据库,其中所有表名都存储在一个表中。 下面是获取表名的方法:

public List<String> getChunks() {
    List<String> chunkNamesList  = new ArrayList<String>();
    Connection chunkTableCon     = DbManager.getBdMergeConnection();
    String[] chunkArray;
    try {
        PreparedStatement cStmnt = chunkTableCon.prepareStatement(chunkNames);
        ResultSet crs            = cStmnt.executeQuery();
        while(crs.next()) {
                chunkArray = crs.getString(1).split("\\.");
                chunkNamesList.add("analytics." + chunkArray[1] + "," + crs.getString(1) + ":" + crs.getString(2));
        }
        chunkTableCon.close();
        return chunkNamesList;
    } catch(SQLException e) {
        e.printStackTrace();
        return null;
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}

我创建了两个线程,这些线程将从集合中读取数据:chunkNamesList进行分析。 下面是我创建两个线程的方法:

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            gpTableCount   = getGpTableCount();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}).start();
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            hiveTableCount = getHiveTableCount();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

当我提交jar时,方法:getGpTableCount和getHiveTableCount首先从方法:getTables()中获取表详细信息,如下所示:

    public Map<String,String> getGpTableCount() throws SQLException {
        gpDataMap   = new HashMap<String, String>();
        gpTableErrs = new HashMap<String, String>();
        gpTableList     = getChunks();
        Iterator<String> keySetIterator_gpTableList = gpTableList.iterator();
        Connection gpAnalyticsCon       = (Connection) DbManager.getGpConnection();
        PreparedStatement gp_pstmnt     = null;
    ..........................
    ..........................
}
        public Map<String, String> getHiveTableCount() throws IOException, SQLException {
            hiveDataMap     = new HashMap<String, String>();
            hiveTableErrs   = new HashMap<String, String>();
            List<String> hiveTableList      = getChunks();
            Iterator<String> hiveIterator   = hiveTableList.iterator();
            Connection hiveConnection       = DbManager.getHiveConnection();
            PreparedStatement hive_pstmnt   = null;
        ..........................
        ..........................
    }

一旦提交了jar,大多数时候,我会在java.lang.NullPointerException行的方法:getGpTableCount()中获得Iterator<String> keySetIterator_gpTableList = gpTableList.iterator(); 除了:

org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:315)
        at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
        at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168)
        at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:116)
        at com.recordcount.dao.ChunkRecon.getChunks(ChunkRecon.java:82)
        at com.recordcount.dao.ChunkRecon.getGpTableCount(ChunkRecon.java:105)
        at com.recordcount.dao.ChunkRecon$1.run(ChunkRecon.java:224)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Socket closed
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:170)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:140)
        at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:109)
        at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:67)
        at org.postgresql.core.PGStream.receiveChar(PGStream.java:280)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1916)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
        ... 8 more
java.lang.NullPointerException
        at com.recordcount.dao.ChunkRecon.getGpTableCount(ChunkRecon.java:106)
        at com.recordcount.dao.ChunkRecon$1.run(ChunkRecon.java:224)
        at java.lang.Thread.run(Thread.java:745)

以下两行中的任意一条随机发生相同的异常:

Iterator<String> keySetIterator_gpTableList = gpTableList.iterator();
Iterator<String> hiveIterator   = hiveTableList.iterator(); in the method: `getHiveTableCount()`

我全局将hiveTableList和gpTableList声明为

List<String> hiveTableList = new ArrayList<String>();
List<String> gpTableList = new ArrayList<String>();

以下两行分别在各自的方法中发生异常:

        Iterator<String> hiveIterator   = hiveTableList.iterator();
Iterator<String> keySetIterator_gpTableList = gpTableList.iterator();

任何人都可以让我知道如何解决此问题吗?

0 个答案:

没有答案